如果“脚本”标签可以自行删除? [英] Should a "script" tag be allowed to remove itself?

查看:177
本文介绍了如果“脚本”标签可以自行删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在工作场所讨论一些与某些反对行为有关的事情。想要听到你们的意见:

 < html> 
< body>
< div>
测试!
< script> document.body.removeChild(document.getElementsByTagName( DIV)[0]); < /脚本>
< / div>
< / body>
< / html>

上述脚本是否可以正常工作并执行应该做什么?首先,我们来看看这里发生了什么:



我有一个javascript,它位于< div> 元素之内。这个javascript将删除正确的脚本本身存在的div内的子节点。



现在,上述脚本在Firefox,Opera和IE8中工作正常。但是IE6和IE7提供了一个警告,表示他们无法打开该页面。



我们不用讨论IE应该如何处理这个问题(他们已将它接受为bug,因此在IE8中修复它)。这里的一点是因为'SCRIPT'标签本身是DOM的一部分,是否允许这样做?在这种操作之后是否存在?



编辑



如果我运行上面的代码,Firefox,Opera,IE9等不会删除'script'标签。但是,document.getElementsByTagName('script')。length返回0!



要了解我的意思,请添加 alert(document.getElementsByTagName('script')。length); before之后的 document.body.removeChild(document.getElementsByTagName('div')[0]);

解决方案

一个例子,自我删除可以方便的是,例如,一些长轮询技术,这将在几分钟后使DOM变得很大。



JavaScript框架添加和删除脚本元素为他们日常生活的一部分。



getScript() jQuery 将脚本标签放在文档头中,并在评估后将其删除。



他们这样做可以保持DOM清洁,否则标签只会建立不必要地。
评估后不再需要它们。



我看到的唯一的缺点是调试 - 例如,Firebug的Firefox似乎丢失了这样的脚本的下落,找不到源代码行。
(撰写本文时)。


We've been having a discussion at our workplace on this with some for and some against the behavior. Wanted to hear views from you guys :

<html>
<body>
<div>
Test!
<script> document.body.removeChild(document.getElementsByTagName('div')[0]); </script>
</div>
</body>
</html>

Should the above script work and do what it's supposed to do? First, let's see what's happening here :

I have a javascript that's inside the <div> element. This javascript will delete the child node within body which happens to hold the div inside which the script itself exists.

Now, the above script works fine in Firefox, Opera and IE8. But IE6 and IE7 give an alert saying they cannot open the page.

Let's not debate on how IE should have handled this (they've accepted it as a bug and hence fixed it in IE8). The point here is since the 'SCRIPT' tag itself is a part of DOM, should it be allowed to do something like this? Should it even exist after such an operation?

Edit:

Firefox, Opera, IE9 etc. do not remove the 'script' tag if I run the above code. But, document.getElementsByTagName('script').length returns 0!

To understand what I mean, add alert(document.getElementsByTagName('script').length); before and after document.body.removeChild(document.getElementsByTagName('div')[0]); in the code above.

解决方案

An example where self removal can be handy is, for example, some long polling techniques that would certainly make the DOM large after a few minutes.

JavaScript frameworks add and remove script elements as part of their daily routine.

E.g. getScript() in jQuery places a script tag in the document head and removes it right after evaluation.

They do this to keep the DOM clean - else the tags would just build up unnecessarily. They are no longer needed after evaluation.

The only drawback I see with this is debugging - for example, Firefox with Firebug seems to be lost as to the whereabouts of such scripts and cannot find the source lines. (As of this writing.)

这篇关于如果“脚本”标签可以自行删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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