Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pre-load in Cache

Status
Not open for further replies.

Splittle

ISP
Jun 21, 2005
26
GB
Hi, All

I have some *.js files that i'm using to hold data that is sent to a div tag using innerHTML, I'm using the onclick event to change the name of the *.js file so that it loads new data i.e ( onclick='rtf.src=file2.js';getdata(); ) changing it to file2 from file1, but the trouble is when it's the first time you load the page it takes two clicks for the data in innerHTML to change, but once you've clicked on everyone of them it will change with one click. This makes me think the *.js files need to be loaded into the browser cache first, maybe i'm wrong, but any advice is good.

thanks.
 

If the .js links are both loaded in the <head> of the page, then they will be in the browser cache once the HTML page has loaded (the onload event has triggered on the <body> of the HTML page).

I would imagine your problem is elsewhere... most likely a variable not being initialised (or the test for a variable whose default value is not set correctly).

Hope that helps.
Jeff

 
there is only the one <script src=file1.js> in the head of the html page, i'm using on click to change the name of the src, also the whole page does get reloaded as i'm using a div's innerHTML and that all that changes in the page
 
If I loaded them all i'd more than likely get errors or it would just look a mess because the *.js files look like this..

file1.js

var data="lots of text";

file2.js

var data="lots of text";

and file3.js and so on, i'm sure you can see a theme forming.

thanks for the reply by the way.
 
Load all the .js files... with the last one in the list being the one you want to be the default:

fileX.js
...
file3.js
file2.js
file1.js

Of course... just try it by adding file2.js before file1.js with the setup you currently have. Does it work ok? There is all the chance in the world it won't... but it might [smile]

Would love to know how you go!
Jeff

 
I just tried your suggestion and something bad must of happened because the first file ''file1.js'' loaded as it should just like you said, but all the other *.js file would not work, I then tried to sneek them in to the cache by putting them in back ground sound tags...
( <bgsound src="file2.js"> ), thought it was worth a try but it also failed. Where theres a will theres a way, i guess i'll keep searching.

Thanks Jeff
 
This is to show the working of it. Let's say two .js, src_1.js and src_2.js each with a simple variable a defined.
Code:
//src_1.js
var a=1;
Code:
//src_2.js
var a=2;
The html page being this.
Code:
<html>
<head>
<script type="text/javascript" id="myscript" src="src_1.js" />
<script language="javascript">
var a=3;	//src load latest; hence this would be overridden
</script>
</head>
<body onload="document.getElementById('mydiv').innerHTML=a;">
<button onclick="document.getElementById('myscript').src='src_2.js';document.getElementById('mydiv').innerHTML=a;">change js source</button>
<br />
<div id="mydiv"></div>
</body>
</html>
Then the first load will show a being equal to 1 (from src_1.js) as the page's declaration a=3 will be replaced by the later loading in real time of src_1.js.

After the button is clicked, the a will be set to 2 showing src_2.js is in effect.
 
Code:
//+--------------------------------------------+
//|File: WebOs.js
//|Date: 10 April 05
//|This file contains code for automatically loading external js files
//+--------------------------------------------+

//-------------[GLOBAL MACROS]
gf_isStr = function(loa_o) {return (typeof(loa_o)=='string');}
//-------------
gf_isInt = function(loa_o) {return (typeof(loa_o)=='integer');}
//-------------
gf_isObj = function(loa_o) {return (typeof(loa_o)=='object');}
//-------------
gf_isDef = function(loa_o) {return (typeof(loa_o)!='undefined');}
//-------------
gf_find = function (t,o)
    {
    for (var x=0; x<t.length; x++)
        {
        if (t[x]==o)
            { return x; }
        }
    return -1;
    }
    
var IJS_Files = new Array();
var IJS_Heads = new Array();
//+--------------------------------------------+
Include_JS = function (Top,fname)
    {
    var head,loc,fArr,el;
    head = Top.document.getElementsByTagName('HEAD')[0];
    if (gf_isObj(head))
        {
        loc = gf_find(IJS_Heads,head);
        if (loc>-1)
            {
            fArr = IJS_Files[loc];
            loc = gf_find(fArr,fname);
            if (loc==-1)
                {
                fArr.push(fname);
                el = Top.document.createElement('SCRIPT');
                if (gf_isObj(el))
                   {
                   el.src = fname;
                   el.type = "text/javascript";
                   head.appendChild(el);                
                   }
                }
            } else
            {
            IJS_Heads.push(head);
            IJS_Files.push(new Array(fname));
            el = Top.document.createElement('SCRIPT');
            if (gf_isObj(el))
                {
                el.src= fname;
                el.type = "text/javascript";
                head.appendChild(el);                
                }
            }
        }
    }
//-----[Example Usage]
/*
Include_JS(top,'macros.js');

---------------------------
WORD OR VOTE TO THE WISE IS ENUFF...;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top