Click to See Complete Forum and Search --> : Can I load a script stored on the server only when I need it?
chestertb
10-12-2003, 02:32 AM
Hi all,
I'm trying to speed up the loading of my site, and it occurred to me that one way to do that is to cut down on the size of the .js files I open when I open a page.
That means only including the universal functions in the script files I open in the header.
I'd then like to be able to access other functions from other .js files on the server ONLY when I need them by calling them from within one of my core functions.
Can this be done?
If not, can I create a text variable and give it an src in the same way that I can create an image variable?
Thanks
IB
Khalid Ali
10-12-2003, 09:14 AM
Probably you ca work somethign out for this purpose,however there is a serious problem here.
When page loads the browser loads all of the javascript variables and functions in the memory,and if you have a function decalred somewhere and not defined you will get a runtime error
"object expected"...so that pretty much will ruin the page....
AdamGundry
10-12-2003, 12:13 PM
An "object expected" error is only thrown when the line is executed, so you should be able to work around it. Something like this might work:
if (!myCoolFunction) document.write('<script type="text/javascript" src="myCoolFunction.js"></script>');
That should load the file "myCoolFunction.js" is the function "myCoolFunction" is not declared. However, it needs to be called when the page is rendering (i.e. in the initial loading before onload fires).
Adam
Khalid Ali
10-12-2003, 12:50 PM
Originally posted by AdamGundry
An "object expected" error is only thrown when the line is executed,
Adam
True...
I am just wondering if he/she was to call this function after the page is loaded, that is by clciking a button or link, then what will be the result of document.write??
;)
AdamGundry
10-12-2003, 01:32 PM
It will call document.open() automatically, which will clear the current contents of the page (not a good thing, most likely).
If this is a problem you might be able to create a script element using innerHTML or DOM methods, but I don't know whether it would work.
Adam
chestertb
10-12-2003, 09:55 PM
Thanks guys.
The page is was originally designed to use innerHTML. I was going to read in the additional js files or text files to add that dynamic content However, as the functionality has grown, the size of the variable file has grown correspondingly this approach has become less and less practical. And taking 3k of HTML and parsing it to escape quotes and semicolons means it's not going to be a very friendly site to work on.
I had this "light bulb moment" at about 3am...
What if I use <iframe>, and load/unload pages and scripts as required into that frame. (yes... old browser issues... and a very small percentage of likely visitors so am prepared to sacrifice those visitors to get the page working for the rest.)
I've tried it, and the theory seems to work, but the execution seems to create javascript/iframe conflicts.
If I open an iframe, are the functions and variables in the parent page available to the contents of the iframe, and then, how do I dynamically get the variables that have been changed in the iframe back to the parent so that, for example, an image in the parent changes on a click in the iframe?
IB