Javascript添加< script>页面加载后的标签 [英] Javascript adding <script> tag after page loads

查看:92
本文介绍了Javascript添加< script>页面加载后的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个小问题.基本上,我试图在页面加载后添加脚本标签.

Got a little problem here. Basically, I'm trying to add a script tag after the page loads.

这就是我正在做的:

index.php:

index.php:

<html>
<head>
   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
   <script>
   function getad()
   {
        $.post('assets/getad.php', "ad", function(response) {
            response = response.replace('document.write','document.getElementById("ad").innerHTML = ');
            eval(response);
            console.log(response);
        });
   }
   getad();
   </script>    
<div id="ad"></div>
</body>
</html>

getad.php:

getad.php:

<?php
    echo file_get_contents("http://ads1.qadabra.com/t?id=a823aca3-9e3c-4ddd-a0cc-14b497cad85b&size=300x250");
?>

您可以在此处找到演示: http://dev.cj.gy/game/

You can find a demo here: http://dev.cj.gy/game/

如您所见,#ad div确实填充了正确的script标记,但实际上并未运行.如果我编辑页面以在页面加载时立即包含script标记,它将运行.

As you can see, the #ad div DOES get filled with the correct script tag, but it doesnt actually run, If I edit the page to include the script tag right at page load, it does run.

推荐答案

是的,<script>标记在作为主文档的一部分进行解析时会导致执行.他们不会因为被写入innerHTML而执行.

Yes, <script> tags cause execution when parsed as part of the main document; they don't execute from being written to innerHTML.

您可以使用调用createElement('script'),设置其src/content并将其添加到文档中的DOM方法,在该初始解析之外创建执行脚本元素.这就是jQuery的getScript所做的.

You can create an executing script element outside of that initial parse using the DOM method of calling createElement('script'), setting its src/content and adding it to the document. This is what jQuery's getScript does.

但是,这对您没有多大帮助,因为下一个脚本ads1.qadabra.com正在document.write进入页面,它本身也调用document.write.

However it wouldn't do you much good because the next script, that ads1.qadabra.com is document.writeing to the page, also itself calls document.write.

您可以通过在document.write上分配自己的自定义函数,而不是写到加载页面,而是尝试提取源代码,从而在客户端上解决这两个调用(即不使用getad.php)传递给它的脚本标签的一部分,并将其加载到DOM创建的脚本元素中.

You could work your way around both of these calls at the client side (ie without getad.php), by assigning your own custom function to document.write that, instead of writing to the loading page, attempts to extract the source of the script tag passed to it, and load that in a DOM-created script element.

但是一般来说,这些脚本旨在在文档加载时同步工作.您尝试执行任何试图迫使其以非预期方式运行的操作,这些操作很可能会变得脆弱,并且在广告网络进行任何更改时都无法正常工作.

But in general these are scripts designed to work synchronously at document load time; anything you do to try to force them to run in a way they weren't intended to is likely to be fragile and stop working when the ad network change anything.

如果您想在不暂停加载父文档的情况下加载第三方广告,建议您将其放在iframe中.

If you want to load a third-party ad without pausing the loading of the parent document, I suggest putting it in an iframe.

这篇关于Javascript添加&lt; script&gt;页面加载后的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆