动态插入< script>网页加载时将标签添加到HTML中 [英] Dynamically Inserting <script> tags into HTML on Page Load

查看:100
本文介绍了动态插入< script>网页加载时将标签添加到HTML中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Javascript动态插入Tweetmeme按钮.我目前在整个网站上都使用jQuery.这是我正在使用的脚本.我基本上想循环浏览所有带有类日记条目的博客条目,并将以下JavaScript附加到末尾.该Javascript直接来自tweetmeme.com.但是,这对我不起作用,并且与append()之间的代码有关.它不喜欢第二组脚本标签.

I'm trying to dynamically insert the Tweetmeme button using Javascript. I'm currently using jQuery throughout the site. Here's the script that I'm using. I basically want to cycle through all the blog entries with a class of journal-entry and append the following JavaScript to the end. This Javascript comes straight from tweetmeme.com. This doesn't work for me though and it has something to do with the code between append(). It doesn't like the second set of script tags.

<script type="text/javascript"> 
$(document).ready(function() {

$('.journal-entry').each(function(index) {
    $(this).append('<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>');
   });

});
</script>

感谢您的帮助. 谢谢.

Any help is appreciated. Thanks.

推荐答案

请勿执行此操作.

<script> HTML内容插入DOM是不可靠的:在不同的浏览器中,它们的工作方式略有不同,并且jQuery不会保护您免受差异的影响.

Inserting <script> HTML content into the DOM is unreliable: it works subtly differently in different browsers, and jQuery won't protect you from the differences.

尤其是,编写innerHTML 从不会导致执行<script>元素的内容,但随后会将<script>元素从其所在位置移动到新的父元素(这是jQuery的一部分) append进程)可能使脚本执行,但并非总是执行(取决于浏览器).

In particularly, writing innerHTML never causes a <script> element's content to execute, but subsequently moving the <script> element from where it is to a new parent (which is part of jQuery's append process) may cause the script to execute, but not always (it depends on the browser).

在任何情况下,它永远不会起作用,因为查看button.js时,它正在调用document.write().该函数仅在初始文档解析时调用才有意义;如果事后从事件中调用它,则该调用将仅替换整个页面内容,从而破坏现有页面.调用document.write()的脚本只能在<script>元素的执行路径内部 在文档加载时运行.您根本不能将脚本包含在动态创建的内容中,因为它不是为此设计的.

In any case, it'll never work, because looking at button.js, it is calling document.write(). That function only makes sense to call at initial document parsing time; if you call it from an event afterwards, the call will simply replace the entire page content, destroying your existing page. A script that calls document.write() can only be run at document load time, from inside the execution path of a <script> element. You can't include the script in dynamically-created content at all, because it's not designed for it.

((如果让您感觉更好,它根本就没有设计; button.js脚本是一堆破烂不堪的旧废话,包括不正确的URL转义-使用escape而不是正确的encodeURIComponent-并且缺少HTML转义.最好远离这些白痴.)

(If it makes you feel any better, it's barely designed at all; the button.js script is a badly broken load of old crap, including improper URL-escaping—using escape instead of the correct encodeURIComponent—and missing HTML-escaping. You may be better off well away from these total idiots.)

这篇关于动态插入&lt; script&gt;网页加载时将标签添加到HTML中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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