🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

[web] [JavaScript] IE Does Not Like Online Script References

Started by
10 comments, last by kanzler 16 years, 5 months ago
I'm fairly confident that one of us is misunderstanding something here (or perhaps both), but it seems to me that you're suggesting that I should contain the entire contents of gf.js inside of Shade.js as a string, which would not only be needlessly complicated but would defeat the purpose of having separate headers anyway. Sorry if I misunderstood you.
Advertisement
Quote: Original post by capn_midnight
As I understand it, you have game.htm and Shade.js on one domain and gf.js on another domain? Well then, your problem is actually a feature! You're attempting something called "Cross-Site Scripting" which is specifically disabled as a security threat. It's a shame that FireFox isn't doing the same, actually.

You are wrong. It's fully possible in all major browsers to include &#106avascript files from other domains. XSS has nothing to do with this.

To the OP: Your method of including the script won't work because of network latency. IE returns immediately after your command to include the script and continues to load it in the background. As long as the script isn't loaded completely it's contents are not available to other scripts and IE will complain about "Object expected" when calling the undefined functions.

In short, you need to wait for the browser to fully load the script before continuing. You could use this function:

function loadScript(scriptLocation, callbackFunction){	// Create script element and set it to load the requested script	var scriptEl = window.document.createElement('script');	scriptEl.type = 'text/&#106avascript';<br>	scriptEl.src = scriptLocation;<br><br>	if (callbackFunction)<br>	{<br>		// Function to be called when script has finished loading<br>		var _onFinished = function(_callbackFunction, _scriptLocation)<br>		{<br>			// Invoke the callback function<br>			_callbackFunction(_scriptLocation);<br><br>			// Clean up event handlers (prevents memory leaks due to closures)<br>			this.onreadystatechange = null;<br>			this.onload = null;<br>			this.onerror = null;<br>		};<br><br>		// Set callback for IE: In defiance of MSDN documentation IE's script object has no &#111;nload handler<br>		scriptEl.onreadystatechange = function()<br>			{<br>				if (this.readyState != 'loaded' && this.readyState != 'complete')<br>					return;<br><br>				_onFinished(callbackFunction, scriptLocation);<br>			};<br><br>		// Set callback for W3C-compatible browsers<br>		scriptEl.onload = function()<br>			{<br>				_onFinished(callbackFunction, scriptLocation);<br>			};<br>	}<br><br>	// Add script element to the document's head section<br>	window.document.getElementsByTagName('head')[0].appendChild(scriptEl);<br>};<br></pre><br><br>Just pass in the full location of the script and a callback function. This callback function will be called immediately after the script has been loaded.

This topic is closed to new replies.

Advertisement