Randy Webb wrote:
[...]
Looks like you want to dynamically load a .js file depending on a
variable without reloading the page.
<URL:
http://groups.google.com/group/comp....scoring%3Dd%26 >
Or, a tinyURL:
<URL: http://tinyurl.com/a8kte >
Is a thread where I explained, and got feedback, on how to do what you
are describing.
Randy,
Further to CES's question, I asked a similar question previously
regarding dynamic loading of scripts. This seems to be a favourite of
yours, I was wondering if you can answer it:
<URL:http://groups.google.co.uk/group/comp.lang.javascript/browse_frm/thread/dbe0590360ce7588/0d99f2f0e7dc8dcd?q=greasemonkey&rnum=1#0d99f2f0e7d c8dcd>
In Firefox, dynamic scripts run but the content is not accessible to the
script that loaded it. setTimeout() can be used to break the nexus but
that seems like a real kludge.
Any hints? Is this a Firefox bug?
A simple test case is below. In Firefox, clicking 'load & call' firstly
prints the error message, then the one from test.js. Subsequent clicks
also print the message from the in-page script before the one from the
loaded script.
IE works as expected - the message from the loaded script appears first,
then the one from the function (with no errors).
The 'load & setTimeout' button works as I would expect the first to work
in Firefox (though setting the lag to 0ms usually causes a 'showText is
undefined' error).
The third button is just to test whether showText is currently defined
or not.
<script type="text/javascript">
function addCall(txt){
var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.src = 'test.js';
document.getElementsByTagName('head')[0].appendChild(oScript);
try {
showText(txt);
} catch(e) {
document.getElementById('msg').innerHTML +=
'showText failed...<br>';
}
}
function addTimeout(txt){
var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.src = 'test.js';
document.getElementsByTagName('head')[0].appendChild(oScript);
setTimeout('showText(\''+txt+'\')', 50);
}
</script>
<input type="button" value="load & call"
onclick="addCall('from ' + this.value);">
<br>
<input type="button" value="load & setTimeout"
onclick="addTimeout('from ' + this.value);">
<br>
<input type="button" value="typeof showText"
onclick="alert('typeof showText: ' + typeof showText);">
<div id="msg"></div>
/***********
test.js is:
***********/
function showText(txt)
{
document.getElementById('msg').innerHTML += txt + '<br>';
}
showText('hi from test.js');
--
Rob