Results 1 to 5 of 5

Thread: loading external script each time a function is used?

  1. #1
    Join Date
    Oct 2006
    Location
    Shanghai, China
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default loading external script each time a function is used?

    Hi,

    I know how to use an external javascript file:

    Code:
    <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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's not how to do an external javascript file. The language attribute has been deprecated and the type attribute is required:

    Code:
    <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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2006
    Location
    Shanghai, China
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    That's not how to do an external javascript file. The language attribute has been deprecated and the type attribute is required:
    Thank you

    Quote Originally Posted by jscheuer1 View Post
    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

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Oct 2006
    Location
    Shanghai, China
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    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.

    Quote Originally Posted by jscheuer1 View Post
    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:
    Code:
    <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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •