2

I have this code which is driving me mad.

parent.document.write("<script type='application/javascript' scr='http://backlinker.nl/ip.php?callback=getip'><\/script>");

this works and adds to the head section of the html document :

<script type='application/javascript' scr='http://backlinker.nl/ip.php?callback=getip'></script>

But it does not execute. The difference is when i paste it directly into the head of the html document without the parent.document.write it works AND executes :

    <script type='application/javascript' scr='http://backlinker.nl/ip.php?callback=getip'></script>

gives output in firebug html source code :

<script src="http://backlinker.nl/ip.php?callback=getip" type="application/javascript">
getip({"ip": "195.241.100.221"});
</script>

when loaded trough the parent.document.write function NO IP output :

<script src="http://backlinker.nl/ip.php?callback=getip" type="application/javascript">
</script>

Anybody knows why the parent.document.write function is working (it puts the code into the head html section) but not loads the ip ?!

Ok guys we have an alternative which is working but still not answers the question which drives me mad, why is mine not working ?!

The workaround =

var scriptElement = document.createElement("script");
scriptElement.src = 'http://backlinker.nl/ip.php?callback=getip';
scriptElement.type = "text/javascript"; //specify type so that the browser won't ignore it
var head = document.getElementsByTagName("head")[0] || document.documentElement;

head.appendChild(scriptElement);

r-d-r-b-3
  • 325
  • 2
  • 11

2 Answers2

2

Try:

var scriptElement = document.createElement("script");
scriptElement.src = 'http://backlinker.nl/ip.php?callback=getip';
scriptElement.type = "text/javascript"; //specify type so that the browser won't ignore it
var head = document.getElementsByTagName("head")[0] || document.documentElement;
head.appendChild(scriptElement);
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • Ok, great this works ! But it's still driving me mad. Why is my function inserting the script but not executing. Yours is inserting + working ! – r-d-r-b-3 Aug 17 '13 at 11:07
  • @dr3: browsers will ignore type that it does not understand. Using `application/javascript` may cause the script to be ignored in some older browsers (although it's inserted). This answer uses `text/javascript` instead. – Khanh TO Aug 17 '13 at 13:00
  • Thank you. So if I would change from application to text in my orignal code with parent.document.write it would work ? – r-d-r-b-3 Aug 17 '13 at 19:34
  • @dr3: You could try it, it may work. I'm not sure if document.write triggers a DOM changed event so that the browser will act on it accordingly (like execute script tags). – Khanh TO Aug 18 '13 at 03:00
1

If you want to create script element, load it and execute - try document.createElement instead:

var scriptElement = document.createElement("script");
scriptElement.src = 'http://backlinker.nl/ip.php?callback=getip';
document.body.appendChild(scriptElement);
yname
  • 2,189
  • 13
  • 23
  • And you can find description and explanation of such behavior of document.write here: http://stackoverflow.com/questions/236073/why-split-the-script-tag-when-writing-it-with-document-write – yname Aug 17 '13 at 01:51
  • Thank you but I tried it and it does not do anything. It does not add a script tag to the html code. – r-d-r-b-3 Aug 17 '13 at 01:52
  • I tried to adjust to but still does not work. its driving me crazy. – r-d-r-b-3 Aug 17 '13 at 02:01
  • I've made test of "document.createElement" approach - all works fine. parent.document.write and document.write gives no results. Try to put alert("") to your js to determine is really problem is with loading and executing js code – yname Aug 17 '13 at 02:13
  • Thanks, will try in a minute ! – r-d-r-b-3 Aug 17 '13 at 11:03