37 users online (0 members and 37 guests)  


  Results 1 to 7 of 7

Related

  1. HTML Combo box referencing XML document    Forum: HTML Forum
    Replies: 0
  2. Folder referencing    Forum: HTML Forum
    Replies: 2
  1. #1
    benzden's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Feb 2002
    Location
    San Antonio, Texas
    Posts
    652

    referencing a .js from from within another?

    Given the <SCRIPT>... tags are not to be including within .js files and assuming it is possible to call another (MASTER) .js file from within other .js files that have minor diffences, how would one go about doing that?

    In other words, what is the equivalent to the .htm file
    <SCRIPT language="javascript" src=../jamrent1.js> </SCRIPT>

    tag within another .js file?


    In essence, I have jamrent1.js and jamrent2.js (and possibly jamrent3.js, and so on in the future) which each reference the same functions the the only difference being that jamrent1's calls include the ../ path, jamrent2's the ../../ path, jamrent3's the ../../../ path and so on. It would be far too cumbersome making the path a part of each and every of the thousands of <SCRIPT language="JavaScript">call</SCRIPT> calls within the hundreds of .htm files involved, so doing so from the jamrent1.js etc files makes it easier. Those files include the tags that make the ../, ../../ and ../../../ calls.

  2. #2
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662


    Hate to admit defeat, but I cannot find a way to do this. I just spent 1/2 hour looking around the web for a way...no good.

    The only way around this that I can see is to have all the JS together, and use absolute paths. Might work, without looking at source, cannot be sure.

    An alternative would be to re-write the .js into a php script. It would 'detect' how far down the directory tree you were, and write out the correct JS code. Its a thought.
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  3. #3
    benzden's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Feb 2002
    Location
    San Antonio, Texas
    Posts
    652
    Thanks for the effort. It appears that calling another sub from a sub wasn't considered when JavaScript was designed - don't know if that is a limitation of C++. I do know that I used those type of 'efficiencies' a lot in the machine language bzs.com (Swiss Army Knife of batch file utilities) file I wrote and donated to the world a decade ago and also in the (Power)Basic programs written after that.

    I think the only efficiency inherent in the scheme I've developed is that I can clone the jamrent1.js file to jamrent2.js and then do a search and replace of all ..\ references to ..\..\ and that handles it completely - just have to get my brain used to that mode of development - and remember to do that cloning (backward and forward) when making changes to either the jamrent2.js or jamrent1.js files.

  4. #4
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Try defining the 'path' as a variable , setting it at the top of the page. Then you only have to change a single line per file.

    You could also add 'future-safeing' by setting it so that you could pass the path along as a querystring. You would call it like this -

    Code:
    src="jamrent.js?path=../../../"
    and access it like this -

    Code:
    path = QueryString("path");
    This might come in useful for the next 800+ pages
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  5. #5
    benzden's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Feb 2002
    Location
    San Antonio, Texas
    Posts
    652
    That would be simple enough to use. As it is, each of my files has either

    <SCRIPT LANGUAGE="JavaScript" SRC="jamrent.js"> </SCRIPT>

    (SRC="../jamrent.js" works the same as the above.)

    or

    <SCRIPT LANGUAGE="JavaScript" SRC="../../jamrent2.js"> </SCRIPT>

    immediately before the closing </HEAD> tag. Making those

    <SCRIPT LANGUAGE="JavaScript" SRC="../jamrent.js?path=../"> </SCRIPT>

    or

    <SCRIPT LANGUAGE="JavaScript" SRC="../../jamrent.js?path=../../"> </SCRIPT>

    would simplify matters a whole lot - allowing editing of only one jamrent.js file and merely replacing the unique "2" in jamrent2.js with the additional path query to each of the SCRIPT tag lines. Will test it after this beer wears off later tonight.

    [Edited after the next posting in order to clarify the correct path to the jamrent.js source files.)
    Last edited by benzden; 08-26-2002 at 06:37 PM.

  6. #6
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Oops. You need to add this code to your *.js files for the above to work.

    The functions are a part of my 'library' JS, so I forget they are there..

    Code:
    //
    // QueryString
    //
    
    function QueryString(key)
    {
    	var value = null;
    	for (var i=0;i<QueryString.keys.length;i++)
    	{
    		if (QueryString.keys[i]==key)
    		{
    			value = QueryString.values[i];
    			break;
    		}
    	}
    	return value;
    }
    QueryString.keys = new Array();
    QueryString.values = new Array();
    
    function QueryString_Parse()
    {
    	var query = window.location.search.substring(1);
    	var pairs = query.split("&");
    
    	for (var i=0;i<pairs.length;i++)
    	{
    		var pos = pairs[i].indexOf('=');
    		if (pos >= 0)
    		{
    			var argname = pairs[i].substring(0,pos);
    			var value = pairs[i].substring(pos+1);
    			QueryString.keys[QueryString.keys.length] = argname;
    			QueryString.values[QueryString.values.length] = value;
    		}
    	}
    
    }
    
    QueryString_Parse();
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  7. #7
    benzden's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Feb 2002
    Location
    San Antonio, Texas
    Posts
    652
    Tried it and got null values, so, after a good night's sleep and morning exercises, my brain kicked back into gear. Always have to remember that

    us humans is always smarter than these dumb damn machines (and programs)

    So here's my fix in order to maintain only one file with the functions that require changing or modification. It is a summary of what's also contained in the jamrent.js file at my
    web host server - a copy of which can be obtained by clicking on the copyright symbol in the signature at the bottom of each page newly redesigned to now display via the JavaScript document.write function:


    This MASTER JavaScript file contains 'all' common functions called
    from every other .htm/.html file in the root, first, second and
    other sub-folders at the web host server. The path variable is set
    by the jamroot, jam1, jam2, (jam#) files, each of which must appear
    BEFORE the jamrent.js reference before the </HEAD> tag in each and
    every .htm/.html file calling the JavaScript functions herewith.
    The contents of those files and the .htm file code lines are:

    <SCRIPT LANGUAGE="JavaScript" SRC="jamroot.js"> </SCRIPT>
    <SCRIPT LANGUAGE="JavaScript" SRC="jamrent.js"> </SCRIPT>
    contents: var path ="";

    <SCRIPT LANGUAGE="JavaScript" SRC="../jam1.js"> </SCRIPT>
    <SCRIPT LANGUAGE="JavaScript" SRC="../jamrent.js"> </SCRIPT>
    contents: var path ="../";

    <SCRIPT LANGUAGE="JavaScript" SRC="../../jam2.js"> </SCRIPT>
    <SCRIPT LANGUAGE="JavaScript" SRC="../../jamrent.js"> </SCRIPT>
    contents: var path ="../../";


    The following code line should also be placed after the first
    <BR> or other <SCRIPT> call in each .htm/.html file.

    <NOSCRIPT>Enabling JavaScript will display this page properly.</NOSCRIPT>


    It couldn't be simpler - both to understand and implement.

    QuietDean, you have my undying gratitude for providing the necessary help and answers to get all this accomplished. Had I known how to do this when I began 'webmastering' several years ago, a whole lot of time and effort could have been redirecting toward web content rather than structure.
    Last edited by benzden; 08-27-2002 at 09:06 AM.



Tags for this Thread