我有以下脚本:
<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/