0

I am trying to create line like this one

<script src="https://www.xtradown.com/feeds/posts/default?alt=json-in-script&max-results=200&start-index=5&callback=peter"></script>

using that code

var s = document.createElement("script");
s.type = "text/javascript";
s.src = "https://www.xtradown.com/feeds/posts/default?alt=json-in-script&max-results=200&start-index=5&callback=peter";
    $("body").append(s);

to be able to change it's content like

start-index=5

to any value I want at any time.

the rest of the code is :

<script>
  function peter(e){
    for(i = 0 ; i &lt; e.feed.entry.length ; i++){
        document.write(e.feed.entry[i].title.$t + "<br/>");
    }
  }
</script>

unfortunately, when I executed that code I got this message

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.

please help me understanding what is the problem.

peter hany
  • 43
  • 1
  • 9
  • You said `the rest of the code is`. Does that mean that is the code found at the xtradown.com url? Or does that mean you're executing that script sometime later in your webpage? – wizebin Jun 19 '18 at 22:46
  • no, I mean the last part of the code is – peter hany Jun 19 '18 at 22:49
  • – peter hany Jun 19 '18 at 22:49
  • full code ----- – peter hany Jun 19 '18 at 22:50
  • From what I understand, the peter function already exists on the page and is the callback function to whatever is in the xtradown.com url (you can see it in the query parameters). @peterhany it seems like a round about way of getting a list of the feed, have you tried any other approaches? – Kenneth Jun 19 '18 at 22:54

1 Answers1

1

The error you are seeing is coming from the code inside the for loop of the peter function. According to this answer you can't use document.write() once the document has been parsed, which it has in this case. It looks like you are using jQuery, so try using something like

$("body").append($("<p>").text(e.feed.entry[i].title.$t));

That will manipulate the current DOM and should solve the problem you are seeing.

Kenneth
  • 416
  • 4
  • 13