0

Continued from previous question

I got the error "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."

So I try to use getElementbyId, and then changing the src:

<head>
<script>
  function pos(url)
  {
  var posterior = document.getElementById("post");
  posterior.src = url;
  }
</script>

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
  pos(url);   //Function call
</script>
<script id="post" type="text/javascript" src='http://xxx'></script>

Where should I execute or define the pos(url) function? The error becomes "Cannot set property 'src' of null" or "pos is not defined" if I place them on different sections.

Well If I use $(window).load on the pos function in head, the error becomes "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." again....

Community
  • 1
  • 1
  • Well you are tyring to set the source of an element before it is rendered to the page. So you need to wait to onload, document ready, or place it after the element. – epascarello Jul 14 '16 at 19:08
  • You should not use document.write after the page has loaded. You should be using DOM methods to add the new content. – epascarello Jul 14 '16 at 19:09

0 Answers0