View Full Version : Load external js files with IF
Tails
03-14-2003, 07:52 PM
Is it possible to load another js file upon a true IF statement? I'm trying to write a choose-your-own-path story and I don't want a single page that has every path due to loading times. So instead, I want to go in the direction of the flow and only load files as necessary. document.write doesn't work with the script tag. For an IE 5.0 user-friendly site, what can I do?
liorean
03-14-2003, 08:05 PM
See if this doesn't work for ie5.0w:
var e=document.createElement('script');
e.src=[String url]
e.type='text/javascript'
document.getElementsByTagName('head')[0].appendChild(e);
For ie5m, try:
var q='\u0022'
if(/mac/i.test(navigator.platform)&&/msie/i.test(navigator.userAgent))
document.createElement('div').innerHTML='\u003cscript type='+q+'text/javascript'+q+' src='+q+[String url]+q+'\u003e\u003c/script\u003e'
liorean
03-14-2003, 08:20 PM
You might want to look into a file loading mechanism instead, though:
/***********************\
* Dynamic File Reader *
* by: Liorean *
\***********************/
function File(){
var r;
switch(0+
(1*(typeof document.getElementById!='undefined')+
2*(typeof clientInformation!='undefined')+
4*(typeof netscape!='undefined'))*
(typeof opera=='undefined')){
case 3:
r=new ActiveXObject('Microsoft.XMLHTTP');
break;
case 4:
r=new NN4HttpRequest;
break;
case 5:
r=new XMLHttpRequest;
r.overrideMimeType('text/ xml');
break;
default:
r={
open:function(){},
send:function(){
return false
},
responseText:''
};
break
}
this.prototype=r;
return r;
}
function NN4HttpRequest(){
this.url=this.responseText=''
}
NN4HttpRequest.prototype.open=function(m,u){
this.method=m||'GET';
this.url=u
}
NN4HttpRequest.prototype.send=function(){
if(this.url=='')
return false;
if(!/^[a-z]+:/.test(this.url))
this.url=String(window.location).replace(/\/[^\/]*$/,'/'+this.url);
var l,b;
this.responseText='';
b=new java.io.BufferedReader(
new java.io.InputStreamReader(
new java.net.URL(this.url).openStream()));
while((l=b.readLine())!=null)
this.responseText+=l+'\n';
if(b!=null)
b.close();
return true
}
Using this, moz, iew and nn4 will lbe able to load a textfile into a variable like this:
var f=new File;
f.open('GET', [String url], false);
f.send('');
var fileAsString=f.responseText;
iem doesn't have this type of interface though - you'll have to use img/object or iframe tags to cover that browser.
Opera and Safari/Konqueror can neither load a javascript nor load a file into a string (unless the img/object/iframe approach works for them), so you'll probably not be able to get something to cover them also.
Tails
03-14-2003, 08:28 PM
When you say iem, you mean mac? I am an anti-mac person and my site gets no mac population. Everyone that uses it tells me that it doesn't support javascript in IE and even sent me screenshot to convince me. I meant IE 5.0 for windows. I think I tried createElement once with no luck. What exactly does createElement do in case I get unexpected things like 'undefined'?. But I never saw it get its own properties like type and src assigned last time I tried, so maybe this will work.
liorean
03-14-2003, 08:36 PM
iem=Internet Explorer for Macintosh
Well, what document.createElement does is create an HTML element. That element is in this case a script tag. The script tag of course needs to be given src and type attributes to work. And, finally, you have to insert it into the head element.
code_stretch
02-12-2009, 02:07 AM
Holy ...
Nice one, this external file access. I was aware xml files could be read but scaling its heirarchy without getting a headache is beyond me. The File( ) function delivers the goods in a flash. My use is on my own machine, not a url on the net, which makes comments about how unlikely it is for JS to read external files out of security obstacles, look out of place.
Unfortunately I can't get it to work on Opera 8.7 that's running on Win CE Mobile 6.0 Japanese in a mobile phone, where I need only to detect the existence of files rather than read them. But Flash shared objects is working well there, so if I can get Flash to talk to JS (Flash's fscommand isn't yet working but might), maybe its errors on finding or not finding files will give me the ability to script for it.
Tails
02-12-2009, 02:58 AM
Wow, that up there was me from so many years ago. When I first learned javascript from tutorials online, I was taught IE-only coding which led me to be bitter at Mac and Mozilla. Wasn't until much later that I learned of the DOM and other standards. Thanks for putting up with me constructively :).
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.