jQuery 和 document.write

标签 jquery document.write

我有以下脚本:

<script type="text/javascript">
$(document).ready(function(){ 
    document.write("<script type=\"text/javascript\" src=\"http://site.com/script.js\"><\/script>");
});
</script>

当我将其放入页面时,它会重定向到我不想要的脚本。

I want to echo this javascript file where I insert the code above, so it is loaded once the page has. Currently when I am using the script above, it is only displaying the contents of the javascript file, and not the whole page.

我希望它将它放入我放置此脚本的页面中。

我这样做是为了一旦页面加载,其调用的脚本基本上就加载到位,因为当前此脚本导致页面在加载时停滞。

谢谢。

最佳答案

尝试:

$(document).ready(function(){ 
    $('body').append("<script type=\"text/javascript\" src=\"http://site.com/script.js\"><\/script>");
});

document.write 在文档加载之前调用时将添加到文档中,但之后它将替换现有内容。

关于jQuery 和 document.write,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8049229/

相关文章:

javascript - 调整大小事件仅触发一次

javascript - 在 javascript 中附加到网页

javascript - 我应该用什么代替 document.write 弹出窗口

javascript - 如果复选框为 false,则从数组中删除项目

javascript - 我们应该在 Angular js 版本 1.3.14 中使用 $locationProvider 吗

javascript - 如何使用 jQuery 和 css 隐藏元素?

jquery - 如何捕获 jsPlumb-Draggable 事件?

php - 为什么 document.write + echo 不起作用?

javascript - 如果使用 document.write,javascript 是否同步加载?

jquery - 在日历中的特定日期之后更改背景颜色