Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    18
    Member #
    18405
    Hi all, I just discovered a frustrating bug in one of my web applications.

    Back before I knew AJAX I created a page that allowed AJAX like interactivity by creating a script element in the HTML DOM which referenced a PHP file like so:

    function loadSkillNames()
    {

    var selSkillName = document.getElementById("sS");
    var i = selSkillName.length;
    while(i != 0)
    {
    selSkillName.remove(i);
    i--;
    }
    var sSN = document.createElement("script");
    sSN.type = "text/javascript";
    sSN.src = "loadSkillNames.php?user="+ USER + "&pass="+ PASSWORD;

    //calls the script
    document.getElementsByTagName('head')[0].appendChild(sSN);


    }

    The above code removes all the options in a dropdown box. It then creates a script element that references a PHP file and passes the element values. When the script is appended it triggers the PHP code at eventime. The PHP script echoes back javascript to the page allowing it interactivity. This script specifically appends new values from the database to the dropdown box. (The code refreshes the values of a dropdown box from a database).

    The problem is that IE stores the javascript returned the first time the PHP script is executed. The second time it actually doesn't really call the function; it just executes the javascript it remembers from the cache. This is bad because the javascript is dynamically recreated by the PHP. It changes from one instance to another. IE just displays the old values in the dropbox.

    Anyone know a way around this? I need to force IE to actually call the PHP script.

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    You can append a random value to the URL to trick the browser into thinking it's a different resource so it won't cache it.

    Code:
    var url = "/whatever.php?foo=1&bar=2&" + Math.random();
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Junior Member
    Join Date
    Feb 2009
    Posts
    18
    Member #
    18405
    Thanks! I actually didn't use the random function because there is the possibility that the same script will be accessed twice with the same random value. I used an incrementing value instead. Works great!


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:12 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com