...

View Full Version : Conditional Loading of External JS File



johnnyb
04-04-2003, 10:30 PM
Hello,

I'm looking for a way to load and execute an external javascript file only if a certain condition is true, for example, I'm looking for the proper way to execute this pseudo-code:

if a == true {
<script language="javascript" src="http://www.xyz.com/myscript.js"></script>
} else {
document.write("too bad")
}

Can someone let me know how this is done?

thanks,

JB

liorean
04-04-2003, 10:50 PM
Depends - when do you want to write it, while the page is loading or when it's finished loading?

// While loading:
if(a==true){
document.write('\u003cscript language=\u0022javascript\u0022 \
src=\u0022http://www.xyz.com/myscript.js\u0022>\u003c/script>');
}else{
document.write("too bad")
}Or:

// When finished loading:
function fnLoadScript(url){
var
t='text/javascript',
q='\u0022',
d=document,
n=navigator,
e;
if(/mac/i.test(n.platform)&&/msie/i.test(n.userAgent))// if ie5m, do it this way
(d.createElement('div')).innerHTML='\u003cscript type='+q+t+q+' src='+q+url+q+'>\u003c/script>';
else{
(e=d.createElement('script')).src=url;
e.type=t;
d.getElementsByTagName('head')[0].appendChild(e)
}
}

johnnyb
04-04-2003, 10:55 PM
Excellent, thanks.

I will be loading it while the page is loading, so I use the first code.

I do have one question, though - wait! I just answered, u0022 is probably a quotation mark or something.

Thanks again.

JB

liorean
04-04-2003, 11:08 PM
Originally posted by johnnyb
I just answered, u0022 is probably a quotation mark or something.
Congratulations - you just won a shoebox to carry your shoes home in next time you buy shoes - can be cashed out in every shoe shop in town!

(When you put scripts in HTML, you must replace <, ", &amp; with &amp;lt;, &amp;quot;, &amp;amp; or escape them in the script. Not all browsers expand entities in scripts, while most handle unicode escapes in javascript.)

joeframbach
04-09-2003, 02:18 PM
be there a way to remove the script?
i'm programming something like a game console. the user loads a game in the js file.
when the user switches games, can i remove the old one?

ConfusedOfLife
04-09-2003, 02:27 PM
You have to do it dynamically I think. Something like:


sc = document.createElement("script");
sc.src = "something.js";
document.body.appendChild(sc);


You have to remove it later by:


document.body.removeChild(sc);


If you document.write, I don't think you can remove it later, unless you refresh the page.

liorean
04-09-2003, 03:37 PM
You can remove the script element from the DOM tree, you can not, however, undo anything the script did, including declaring functions and variables that didn't get used.

joeframbach
04-09-2003, 04:52 PM
i can remove the script tag, but the functions will still be accessible?

liorean
04-09-2003, 05:45 PM
Yep. That's the way it works. If you want to delete the functions, use delete functionName; or functionName=null;.

johnnyb
05-23-2003, 02:50 AM
So, I'm making some web pages that are viewed locally, (off a CD), but interact with a web server to fill a person's shopping cart.

I am planning on using the method of loading a javascript file after the page has been loaded that lilorean gives near the beginning of this thread, and having the javascript file that is called be an ASP file that adds stuff to the shopping cart based on the querystring that is passed to it, (constructed by javascript on the original page).

While I know that this will work, I am wondering if there is a better way of using local javascript to send commands to a web server. This could get rid of some caching issues in IE.

John

Vladdy
05-23-2003, 03:06 AM
Posted this awhile back:
http://www.codingforums.com/showthread.php?s=&threadid=16205&perpage=15&pagenumber=1
and followed up here:
http://www.codingforums.com/showthread.php?s=&threadid=17308

Jerome
05-23-2003, 07:30 PM
Liorean,

How's browser-support for Your loading mechanisme?

Jerome

liorean
05-23-2003, 09:08 PM
The first? Should have browser support to nn4 and later. As long as the browser supports unicode JavaScript escape sequences it works. You can of course change the escape sequence to be an ASCII hex code instead, but where lies the fun in that?

The second import mechanism, however, only works in moz, iem and iew, unless saf/konq has begun supporting it too. Op7 doesn't support it for two reasons that I won't go into detail on unless you ask for them. That's pretty much all the DOM enabled browsers except for iCab, about which I have no idea.

Jerome
05-23-2003, 10:58 PM
Thanks Liorean!

For option 2, load after the document has finished loading I have 2 questions:

Q1.
In the head of my document I already have a script external-src declaration, no problem with Your loading mechanisme it will append another one, and works fine. The question is how to remove only the one Your mechanisme appends and not the other one or both. Can I give them an ID maybe?

Q2.
Do I need to check if the external file is fully loaded (in the past I used a function for it) to read-out an array or is there like xml-file retrieving a special declaration for it (xml_Doc.readyState==4 which to my idea works well)

Thanks in advance,
Jerome

liorean
05-24-2003, 12:51 AM
A1: Not really sure how you mean - it appends a new script element to the head, and you can remove that script element form the head as you wish using lastChild. However, as I mentioned above, you will only have removed the script tag, you haven't undone anything the script does or defines for later use. This is especially evident in the iem fork, that never even appends the script element to the document.

A2: One of the problems with it is that there is no way for you to know that the file is loaded. That's why you should have all direct calls to it in the file itself or in functions that are called by events/timed calls.

Jerome
05-24-2003, 10:52 AM
Thanks Liorean,

A1.
lastChild, ofcourse. For IEm I can remove the div, given it an ID. This is going to work fine.

One more question about removing the actual script as You mentioned.

The js-files are basicly the same, only contain an array with lets say as name - data - . Before appending a new file I can declare this array - data - being - null -, not? Then we start fresh and clean every time.

A2.
Solved

Jerome

liorean
05-24-2003, 01:46 PM
Originally posted by Jerome
Thanks Liorean,
A1.
lastChild, ofcourse. For IEm I can remove the div, given it an ID. This is going to work fine.Well, as you might notice, the div is created but never inserted in the document, so no, you can't. It's got a refcount of zero, so it's going to be garbage collected as soon as the function has been executed.
One more question about removing the actual script as You mentioned.

The js-files are basicly the same, only contain an array with lets say as name - data - . Before appending a new file I can declare this array - data - being - null -, not? Then we start fresh and clean every time.Yep, that's what you do if you want to "forget" the previous scripts. The bad point about it is, of course, that the script that is cleaning up will have to know about the contents of the script beforehand, so you'ren't able to make the scripts entirely modular.

Jerome
05-24-2003, 05:20 PM
I will not give up!

You mentioned, Your mechanisme doesn't work in Opera 7.0, because of 2 reasons, I am curious!

Do You think security will play a role for the next versions of IE, Mozilla to abandon this way of including external js-files?

And saf/konq (browsers used on linux, not?) is it not possible because of DOM limitations or including js-files?

Thanks for Your time!
Jerome

liorean
05-24-2003, 10:03 PM
No, security will not interfere with this mechanism. It conforms to all security rules as it should. There is a possiblity that it may be opt-out from security and privace preferences, though.

As for Op7:
1. If you use the DOM, for some reason it doesn't set all kinds of attributes. class, id, name etc. can be set, but not src on the script tag. It seems like they have implemented a system that doesn't open the entire DOM to interactions. YOu can get around this by using outerHTML though.
2. Even when you have set the script using outerHTML and thus have it in the source, it doesn't load the file. This is the same behavior as konq had, and maybe saf/konq still have.


Konqueror is a unix/linux browser made by KDE, while Safari is Apple's browser based on the same core components.

You'd have to ask Brothercake or some other mac user about how Safari handles it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum