1

I need to embed a player by the following code:

<script type="text/javascript" src=***the url***></script>

A part in the url is the ID which is retrieved from mySQL database. I know that the string concatenation can't be directly handled in the src, so I add the function in the beginning:

<head>
<script>
function call(url)
{
  var script = document.createElement('script');
  script.type="text/javascript";
  script.src = url;
  document.body.appendChild(script);
}
</script>
</head>

In the body,

<body>
  <?php ***...to retrieve ID***?>
  <script type="text/javascript"> 
    var videoid = "<?php echo $id; ?>";  //Transfer ID to javascript 
    var url="http://......"+videoid;   //Url string finished
    call(url);   //Function call
  </script>

Now the script is appended, but I can't see the player. Why?

  • check the console.is there any error messages? – Madhawa Priyashantha Jul 14 '16 at 17:36
  • If you check the network tab of the browser, is it actually pulling in the js script? – Zach Jul 14 '16 at 17:36
  • I would suggest not using the keyword `call` as a function name. Use `appendScript` or something more obvious... – Heretic Monkey Jul 14 '16 at 17:37
  • Is the DOM inspector displaying the correct URL and script tag? What happens when you copy paste the url from the tag into the browser? – Isabel Inc Jul 14 '16 at 17:38
  • Try this ways http://stackoverflow.com/questions/11150409/use-js-variable-to-set-the-src-attribute-for-script-tag – A l w a y s S u n n y Jul 14 '16 at 17:40
  • If I directly add the full url like the player appears. By the inspector I can see both the scripts of manual url and database url exist, but only 1 player shows (the manually input one). – user6571534 Jul 14 '16 at 17:54
  • I find the following error on console of chrome: "Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened." What does it mean? It seems directly related to the player that is missing. – user6571534 Jul 14 '16 at 17:59

0 Answers0