#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    11
    Rep Power
    0

    Question External JavaScript file from within a JavaScript function?


    Does anyone know how to call an external JavaScript file from within a JavaScript function?

    I know the following works outside a JavaScript function:

    <script src="MyCode.js" type="text/javascript"></script>

    but it does NOT work from within a JavaScript function, as there the code is already in SCRIPT tags.

    Off course I could just include the code in the function, but it's rather large, and I see no need for it to load (saving bandwidth) unless the function is called from the HTML part.

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    There's no include directive...all the script tags have to be in the proper order in the parent document.

    For complicated scripts, you could always test the existance of a certain variable, and if it fails, alert the user, but there's no way to include a file.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    11
    Rep Power
    0

    Jump to an anchor then?


    Is there then a way to jump to an anchor (in the same document), if a certain condition is met, from a JavaScript function?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    11
    Rep Power
    0

    Eureka!!!


    YES - Using the eval function I figured out how to call JavaScript code from within a JavaScript function.

    Now I can truly make my pages dynamic by calling code files by a variable.

    About time, I only worked on this for 12 hours
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0

    Unhappy HOW???!!!!???


    i am extremely interestied in knowing how you managed to pull this off!

    i'm trying to call an external .js file which will be created and named dynamically based on the logged in user's username;

    ie: aaron660.js

    i know how to assign the environment variable for the username to a var in my javascript, but i have to be able to call the specific external javascript file based on this username

    ie: var.js

    can you please email me: aaron@vdj.ca or just post a reply?

    THANKS!
    Last edited by aaron660; April 16th, 2003 at 10:23 AM.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    1
    Rep Power
    0

    interested in the topic


    Got the same problem and ?
    Please e-mail me the tip.
    I want to call js function from js file within another js function in the js file.
    I know the ways to overcome it:
    1) I can merge files.
    2) My first file gets info from XML recordset. I know I can call it in the second file as well to get info.
    But, Is there a way to do that without merging?
    Wondering if there is a way to link files without performing redundant coding.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0
    The solution was right under my nose...

    just use:

    document.write("<script src='var.js'><\/script>");
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0
    This is mostly in response to aaron660's final post "The solution was right under my nose ...":

    What needs also to be stated here (so as not to confuse any "newbie" scripters) is that once an html page's content is processed by the browser (ie: after the closing html tag is read), there can be absolutely no more writing done to that document ... only to objects WITHIN that document, such as layers, forms, etc.

    I say this because should anybody want to call up a "conditional" external .js file DURING the initial document load process (that is, without any delayed back-referencing function calls done, such as through a "setTimeout()" internal function call), then, yes, the document.write("<script src='var.js'><\/script>"); will likely do what you expect.

    However, if after the document is loaded, and you later try to change its .js source file (either conditionally or via some user input or criteria), then all you will manage to do is write to and create a completely blank (and tag-less) document - something I'm sure your visitors will not appreciate.

    Just wanted to clear that up so nobody would end up spending dozens of hours on a hopeless endeavor. Truly the only way to accomplish such "on-the-fly" variable swapping from an external file is by utilizing a custom Java applet ... and that's a whoooole nuther ball game! And forum. Cheers!

    --Mikey
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Posts
    1,014
    Rep Power
    15
    once an html page's content is processed by the browser (ie: after the closing html tag is read), there can be absolutely no more writing done to that document ... only to objects WITHIN that document, such as layers, forms
    Hmm, ever heard of DOM?

    1. Here's the code for a JavaScript include file, let's call her 'dothat.js':
    Code:
    myvariable = 2
    function showfromdothat() {
    	alert (myvariable)
    }
    2. Here's some HTML that will not initially call dothat.js:
    Code:
    <script type="text/javascript">
    var myvariable = 1
    function loadit() {
    	var newscript  = document.createElement("script")
    	newscript.type = "text/javascript"
    	newscript.src  = "dothat.js"
    	document.body.appendChild(newscript)
    }
    var iId = null
    function showmyvariable() {
    	if (typeof showfromdothat != 'undefined') {
    		if (iId != null) {
    			clearInterval(iId)
    			iId = null
    		}
    		alert (myvariable)
    	}
    	else {
    		loadit()
    		iId = setInterval("showmyvariable()",100)
    	}
    }
    </script>
    </head>
    <body>
    <div onclick="alert(myvariable)">initial: myvariable</div>
    <div onclick="showmyvariable()">show myvariable</div>
    </body>
    If you first click on "initial: myvariable", it will show "1", as this is the value defined for 'myvariable' inside the JavaScript for the document.

    Now, if there were 'absolutely no more writing done' to the document, the alert would still say "1" upon clicking 'show myvariable'. Instead, it says "2", the value being loaded from the externally called JavaScript file. This file is read AFTER the document has been fully loaded. The "dothat.js" file is loaded dynamically, upon request...
    (Opera 7 does not like this, IE and Mozilla fully support it...)

    Jeroen
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0

    Thumbs up


    Nice!
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0
    Yes, very well put, jerom! I sometimes live in a bubble, as any script that I write must function in all existing (popular) browsers released since 1995 [such as IE, NS4.x, NS6, Opera, and AOL - which has an IE engine in it] ... and this sort of spilled over into my previous post.

    Having such a staunch "equal viewing experience for all" developer's pov, I sometimes forget about just how versatile dom has become. Opera, on the other hand ... well, it may be fast as the d.i.c.kens, but at what expense? A Ferrari with wooden seats, no stereo, and crank down windows. Sort of takes the fun out of the drive. I wonder if they've managed yet to include a functioning innerHTML feature. Cheers, all!

    --Mikey
    Last edited by Mikey_Doodle; August 19th, 2003 at 03:26 AM.

IMN logo majestic logo threadwatch logo seochat tools logo