[英]javascript document.write loaded but not executed
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 : 这有效并添加到html文档的开头部分:
<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 :
区别在于,当我将其直接粘贴到html文档的开头而没有parent.document.write时,它可以工作并执行:
<script type='application/javascript' scr='http://backlinker.nl/ip.php?callback=getip'></script>
gives output in firebug html source code : 在firebug html源代码中给出输出:
<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 : 通过parent.document.write函数加载时,没有IP输出:
<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 ?! 任何人都知道为什么parent.document.write函数可以正常工作(它将代码放入html的开头部分)但不加载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); head.appendChild(scriptElement);
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);
If you want to create script element, load it and execute - try document.createElement instead: 如果要创建脚本元素,请加载并执行-尝试使用document.createElement:
var scriptElement = document.createElement("script");
scriptElement.src = 'http://backlinker.nl/ip.php?callback=getip';
document.body.appendChild(scriptElement);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.