Passing variables to view.js from view.php

Permalink 1 user found helpful
Hello all,
I have a simple block that displays a video with a thumbnail. Everything works fine. But due to the way the vimeo player does not respect the css (i.e. youtube player does not autoplay when display=none, vimeo play does, and that is a problem) I thought to simply have a some javascript inject the div with the iframe code on onClick.

Whether or not this is the best way is not the question. The questions is, it seems I need to put whatever javascript I have into view.js, as javascript in the view.php does nothing. No problem, but as the iframe code uses variables to define WxH, i need some way to pass the variable values from view.php to view.js.

Perhaps I need to use custom data attributes?

Or, do I have run the php in the view.js to get the values

I can add php code in the view.js, and I can’t add js in the view.php (which is a mystery to me, but nothing I add does anything)

I know there IS a way to do this using another method, like defining a function in the head and passing arguments from the view.php, in onClick, but it just would be ‘cleaner’ if I could pass to view.js, I think.

Thanks

ntisithoj
 
JohntheFish replied on at Permalink Reply
JohntheFish
You can output script tags and js in a view.php.
ntisithoj replied on at Permalink Reply
ntisithoj
I get that, which is why none of the scripts worked in the view.php... but would/shoud I use javascript data elements to pass to view.js, or out my js functions in the head and call that function from the view.php? I am wondering what the 'best' way is.
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
I use both, depending on what i am doing. Sometimes it makes more sense to build js from php in view.php and echo the data. Sometimes it makes more sense to echo data- elements into the html in the php and let the js read it. There are many variations.

The main thing to beware of and to protect against is clashes between the js when there is more than one block on a page, especially with copy/pasted blocks (that will have the same bID)