|
.g.e.n.e.r.a.l.
.p.h.p. .c.o.d.e.
.j.a.v.a.s.c.r.i.p.t. .c.o.d.e.
Modified in < 3 days.
Misc. Stats For This Page
Operating Systems
Browsers
|
.l.o.a.d. .e.x.t.e.r.n.a.l. .v.a.r.i.a.b.l.e.s.
Have you ever needed to load a JavaScript variable from an external file from within a
block of JavaScript (i.e. not using a <script src=???></script> in
the header)? "No, why would you want to do that?" you say. Let's take look at
the following example:
<script>
Now you might say "Why not load my_id from the external file every time the page loads?". Multiple reasons: server load, session tracking, etc., etc. The trick is getting the JavaScript to load this external file in the middle of execution. You could try using the eval() statement but I have had mixed results with that. So how else could this be accomplished? The answer is use the DOM. By using the DOM, we can add a <script src=???></script> in the head before or after the page has completely loaded. Here's the code:
<script>
The important thing to remember here is that even though you can add the code above the current running section of code, it does not necessarily mean that any variables in loaded in that code are currently available to the script. Sometimes they are sometimes they are not, it really depends on the platform/browser in question. In other words don't trust it. Generally, the onLoad event has not fired yet so you could use it to time when the variables are read. |
||||||||||||||||||||||||||
|
The trademarks, logos and service marks ("Marks") displayed on this Web site are the property of cKorp or other third parties.
You are not permitted to use these Marks without the prior written consent of cKorp or such third party which may own the Mark.
Use of cKorp trademarks almost always requires written permission or a license.
|
|||||||||||||||||||||||||||