【发布时间】:2012-11-22 12:36:58
【问题描述】:
这可能是不可能的,但值得一问。我正在将脚本标签动态插入到我页面的head 部分。目前我正在使用document.write,你们中的许多人会皱眉头,但它确实可以很好地完成工作。
但是,出于here 所述的原因,我想找到document.write 的替代方案。
因此,我需要一个可以做到这一点的函数:
<!-- The document.write writes the new script here synchronously so it will block other scripts -->
<script type="text/javascript">
// Code here that uses the code from the dynamically inserted script
</script>
任何人都可以建议任何使用 jQuery 或纯 javascript 将在页面上插入元素的东西,但也符合这些要求:
- 将元素放置在调用函数的位置。例如脚本标签被放置在调用它的脚本标签之后
- 脚本会同步加载,因此会阻塞其他脚本,直到它完成
Google 使用他们的Google.load() 方法来做到这一点,他们使用 document.write 吗?肯定不是……
【问题讨论】:
标签: javascript jquery sinemacula