peter hany
peter hany

Reputation: 43

update json file content with js callback function

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.

Upvotes: 0

Views: 249

Answers (1)

Kenneth
Kenneth

Reputation: 415

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.

Upvotes: 1

Related Questions