user3694193 user3694193 - 1 year ago 59
PHP Question

Pass variable to javascript and calling

I have a code with javascript to play youtube videos instead of iframe but i don't know how do i pass the video id from a php script variable or a bbcode like [youtube]videoid[/youtube]
I just need to pass down the video code to this function.So please help me and maybe this may have discussed here but i could find (maybe i don't know what exactly i have to search for)
Here is the code


var tag = document.createElement('script');

tag.src = "";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE', // remove if assign in php
events: {


Or assign videoId: in php.

Answer Source
videoId: <?php echo json_encode($videoId); ?>,

JSON-encoding is compatible with JavaScript and will take care of any escaping and what not that may need to be done.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download