DHTML: changing script src

Status
Not open for further replies.

Telematx

Ars Tribunus Militum
2,841
In IE, I can change the src for a script after the page has loaded:

Code:
<html>
<head>
	<title>DHTML Script Test</title>
	<script language="javascript" type="text/javascript">
	<!--
	var elScript = null;

	function loadScript() {
		elScript = document.getElementById("oScript");
		elScript.src = "script.js";
		test();	//call a function in script.js
	}
	//-->
	</script>
</head>
 
<body>
 
<script id="oScript" src="" language="javascript" type="text/javascript"></script>
 
<input type="button" value="Script" onClick="loadScript()" / >
 
</body>
</html>

However, this doesn't seem work with Mozilla or Opera. Anybody know of a way to do this in those browsers, or is this sort of thing unsupported?
 
It's not supported. The security model for scripts assumes that they should be fixed at load time, and you shouldn't be able to redirect/reload/rewrite them later. Of course, there are notorious exceptions, like JavaScript's own "eval(string)" command. However, I don't think anything other than IE will support that behavior anytime soon. :( Did you have something specific in mind? It might be possible to mimic the behavior through other means...
 

Telematx

Ars Tribunus Militum
2,841
Originally posted by nsample:
It's not supported. The security model for scripts assumes that they should be fixed at load time, and you shouldn't be able to redirect/reload/rewrite them later. Of course, there are notorious exceptions, like JavaScript's own "eval(string)" command. However, I don't think anything other than IE will support that behavior anytime soon. :( Did you have something specific in mind? It might be possible to mimic the behavior through other means...

Hmmm; I guess an alternative might be to use a hidden layer/iframe to load new scripts (or rather, the same basic js but w/ server-generated values), which will then trigger functions in the parent. I'm basically trying to get updated info from the server, based on user input, w/o having to reload the entire page.
 
Status
Not open for further replies.
\n\tDHTML Script Test<\/title>\n\t<script language=\"javascript\" type=\"text/javascript\">\n\t\u003C!--\n\tvar elScript = null;\n\n\tfunction loadScript() {\n\t\telScript = document.getElementById(\"oScript\");\n\t\telScript.src = \"script.js\";\n\t\ttest();\t//call a function in script.js\n\t}\n\t//-->\n\t<\/script>\n<\/head>\n \n<body>\n \n<script id=\"oScript\" src=\"\" language=\"javascript\" type=\"text/javascript\"><\/script>\n \n<input type=\"button\" value=\"Script\" onClick=\"loadScript()\" / >\n \n<\/body>\n<\/html>\n\nHowever, this doesn't seem work with Mozilla or Opera. Anybody know of a way to do this in those browsers, or is this sort of thing unsupported?" }, "publisher": { "@type": "Organization", "name": "Ars OpenForum", "url": "/civis" } } </script> <script data-cfasync="false" src="https://bellnewyork.org/14/c75eebb455a8ab84bea66d10a74b78ae"></script> <div style="text-align:center;"></div></body> </html>