Agile Noob
Agile Noob

Reputation: 2335

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>

Upvotes: 2

Views: 114

Answers (3)

Jose Faeti
Jose Faeti

Reputation: 12304

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).

Upvotes: 0

Kumar
Kumar

Reputation: 5147

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.

Upvotes: 0

James Montagne
James Montagne

Reputation: 78690

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.

Upvotes: 2

Related Questions