johnjohn
johnjohn

Reputation: 4281

How does a browser include a javascript file?

When you add a script tag with an src field pointing to a javascript file, the browser gets it and executes it.

What I am wondering is how does this process work. Does the file get fetched and then somehow eval()'ed? Is it a mystical process that happens outside of the scope we have access? Or can we monitor/interfere this?

To explain what I want to achieve: I want to give access to third parties to customize their page on my site by linking to their own javascript files. I want to be able to centrally log any errors that prevent those external scripts from loading (i.e. their server is down, parsing errors, whatever). Is it possible?

Thanks!

Upvotes: 1

Views: 108

Answers (2)

Ilya Kozhevnikov
Ilya Kozhevnikov

Reputation: 10432

You can look at specific implementation in Firefox & Chromium sources with some grep magic, for your specific scenario I'd asynchronously subload those by inserting script tags on the fly and listening to global errors/events (eg window.onerror) from there.

Upvotes: 3

Bhavik Ambani
Bhavik Ambani

Reputation: 6657

It loads the java script file while loading the page and stores that script in its cache. If you load page another time then it will get that js from the cache.

Upvotes: 0

Related Questions