PDA

View Full Version : loading external script each time a function is used?



kasei
09-05-2007, 04:25 AM
Hi,

I know how to use an external javascript file:


<script
language="javascript" src="external_file.js"></script>

The thing is that some of the variables will be constantly changing (via php).

JavaScript doesn't seem to have anything equivilant to PHP's file() or file_get_contents() functions for local files, right? So I thought I could put the php on an external javascript code and have it loaded via a function.

Is there a way to load a script from an external file (located locally) each time a function is used?

Thanks,

Joe

jscheuer1
09-05-2007, 05:37 AM
That's not how to do an external javascript file. The language attribute has been deprecated and the type attribute is required:


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

Now, not even a normal PHP page will receive constant updates via PHP from the server. If something happens that needs to be reflected to the user, the page must either be changed or reloaded.

What you might not realize though is that you can have an external javascript with the .php extension, so that every time it is served, it can have parts of itself updated from the server. This can be tricky on the user end, as the old version may be cached. A unique, but otherwise unused query may be attached to the filename each time it is served to prevent caching on the user end. Also, an external javascript written in part in PHP and using the .php extension could receive information via the query string.

kasei
09-05-2007, 04:59 PM
That's not how to do an external javascript file. The language attribute has been deprecated and the type attribute is required:

Thank you :)


What you might not realize though is that you can have an external javascript with the .php extension, so that every time it is served, it can have parts of itself updated from the server.

This is exactly what I wanted to do. I just wanted to be as un-confusing as possible.

Now, I'm pretty fair with php, but I'm at "baby-talk" level with JavaScript. If I call an external file with the src attribute in the <script> tag, it will only load when the page loads, right? and if I want to call it again I must reload the page?

So what I want to know is, what methods are avalible in JS to either:
1. call an exteral script using the script or a function, or
2. read the text on a page using the script or a function.

Either one will work. Like I said before the page that needs to be loaded is in the same directory as this page.

Again, thanks,

Joe

jscheuer1
09-05-2007, 05:19 PM
OK, the easiest way would be to reload the page. You could change the src attribute to the script tag via javascript, but as previously mentioned, to ensure that it's served anew, a unique query value would have to be appended to the filename by javascript (if changing the attribute via javascript) or by PHP (if reloading the page). Depending upon the type of script, what it does and how, this may still not be enough.

Since you appear to be depending upon javascript here, why not just have whatever changes the server side value also change it on the client side? And, you should realize (if you don't already) that depending upon javascript for anything that involves the server side is potentially both insecure and unreliable. First because javascript can be altered by the client, and second because the client may not have javascript enabled.

kasei
09-05-2007, 07:36 PM
OK, the easiest way would be to reload the page.

Well, this is for an RPG game. I already have a page version that is 100% php which works by reloading frames. The reason I want to do it via JS is to avoid loading all of the code and images every time they click on the page in order to speed the game up from the player's perspective and reduce bandwidth (and lag) from mine.


Since you appear to be depending upon javascript here, why not just have whatever changes the server side value also change it on the client side? And, you should realize (if you don't already) that depending upon javascript for anything that involves the server side is potentially both insecure and unreliable. First because javascript can be altered by the client, and second because the client may not have javascript enabled.

Yes. I understand. If they have javascript disabled then they will be directed to the 100% php version.

What I have so far is this:

<script type='text/javascript' /><!--
function IncludeJavaScript(jsFile)
{
document.write('<script type="text/javascript" src="'
+ jsFile + '"></script>');
}
//--></script>
<a onclick="IncludeJavaScript('index2.script1.php?msg=Hi')">Hi</a>
<a onclick="IncludeJavaScript('index2.script1.php?msg=Bye')">Bye</a>

index2.script1.php just contains an alert that displays $msg.

The problem is that when I click on the text the alert comes up but the page also goes white like it's reloading.