开发者

Will an error in an external javascript from a separate domain break the javascript on my site/domain?

My concern is my site pulls in ads dynamically from several differnt ad vendors. So say there is an ad from the third party's javascript on a separate domain, could that then break the javascript on my site/domain? I believe that javascript is encapsulated by domain but just wanted to make sure.

http://mysite.com
<script type="text\javascript" src="foo.com">
cause some error...
</script>
<script type="开发者_运维知识库text\javascript" src="http://www.mysite.com/js/fooscript.js">
document.write("write something")
</script>
<script type="text\javascript" src="http://www.mysite.com/js/fooscript.js">
cause some other error...
</script>
<script type="text\javascript" src="http://www.mysite.com/js/fooscript.js">
document.write("write something else")
</script>


If the script involves any kind of global variable or function, you always run the risk of naming conflicts. A well written library will minimize this risk as much as possible by avoiding lots of variables in the global space.


I like your question. This reminds me of a talk where the speaker wanted us to use a namespace for all our JS. If one namespace has some problem then other namspaces are least affected until unless they use same resources. This is the same reason why you should not modify the default behaviour of objects and extend/create your own methods objects. So in your case, if a JS is modifying any global object which in turn is being used by your scripts, the break down in that script, IMHO, should affect your script as well.


Even if you are loading different scripts stored in different domains, the code will always run on the same page of your browser, there are no other environments, unless you are using iframes.

An error raised by any one of the scripts will be thrown in your current JavaScript environment, hence other scripts may stop working or don't work correctly (this also depends on browser used).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜