First of all, thank you for the answer, Martin!
> Does test.js at least contain code that is executed during page load?
Yes, I can write anything I want in test.js. Even more, I can change the way
the test.js is called:
1. <div>
2. <script type="text/javascript" src="test.js" /></script>
3. </div>
Lines 1 and 3 are out of my control, but I can write anything in line 2, for
instance:
2a. <script>
2b. document.write('<scr'+'ipt type="text/javascript" src="test.js">' +
2c. '</script>');
2d. </script>
> If so you could try e.g.
> var scripts = document.getElementsByTagName('script');
> // during page load the current script element should
> // be last in scripts
> var lastScript = scripts[scripts.length - 1];
> lastScript.parentNode.style.backgroundColor = 'red';
Is this cross-browser or could there be some race conditions? In other
words, is there a case when script loading is deferred?
I would expect browsers to load other elements even if the script is not
loaded yet, and in that case some other scripts might load before ours. But
I guess the scripts would be added in DOM tree right before they are
executed?
One idea I got in the mean time was this:
2a. var scr = document.createElement("script");
2b. scr.type="text/javascript";
2c. scr.src = "test.js";
2d. document.body.appendChild(scr);
And then in test.js:
scr.parentNode.style.backgroundColor='#ff0000';
(uses global variable scr)
Would this work? Is it cross-browser?
Thank you again, appreciate it!