SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast Pramit's Avatar
    Join Date
    Feb 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX URL Problem

    Hi!
    In AJAX, after I call a new page and then refresh the page, original file is restored. I need a way to dynamically change the url when a new file is called. I tried using #. But I cant solve the problem.
    Please help.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You can't change the URL except for adding # and something after it. If you get something via AJAX and then set window.location to something other than the current page (with or without #...) then the browser will go to that location.

    You could add #... and then when the user reloads the page (including #...) you then have some javascript that knows to load the AJAX stuff automatically without any user interaction.

    However, this is fragile and you'd be better off leaving an indicator on the server. When you call the stuff with AJAX, something is stored on the server so that when the page is requested again (when you reload the page) it can serve the new content and not the original content.

  3. #3
    SitePoint Enthusiast Pramit's Avatar
    Join Date
    Feb 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya I tries window.location and window.hash too. but it will simply refresh the page. I dont know whether the code is not working or I am making some mistake. can u show me some sample code?

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sample code for what? I'm not too sure what you want. window.location of course refreshes the page, that's what it's for. window.location.hash (not window.hash) changes the stuff after # (without reloading the page), but I think that in IE7 setting this doesn't add the new URL to the history, which could be irritating for users. Other than the hash, you can't change anything else in the address bar without reloading the page or sending the browser somewhere else (it would be a security problem).

  5. #5
    SitePoint Enthusiast Pramit's Avatar
    Join Date
    Feb 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    function ShiftChanger(divId,url,val) {
    if(enableCache && jsCache[url]){
    document.getElementById(divId).innerHTML = jsCache[url];
    return;
    }
    var ajaxIndex = AjaxObjects.length;
    AjaxObjects[ajaxIndex] = new sack();
    AjaxObjects[ajaxIndex].requestFile = url+"?val="+val;
    //document.getElementById("ajax-loader").innerHTML = '<img src=images/ajax-loader.gif />';
    window.location.hash = "index.php#"+val;
    AjaxObjects[ajaxIndex].onCompletion = function(){ ShowContent(divId,ajaxIndex,url+"?val="+val); };
    AjaxObjects[ajaxIndex].runAJAX();
    }

    I did this. the url changes. but when i refresh the page, original data is restored.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    When you set window.location.hash, you only need the stuff after the #. So:
    Code:
    window.location.hash = val;
    In Firefox it appears you can include the # or not, I don't know about other browsers (test it).

    The original data is restored because changing the hash does nothing except scroll the page to the place where there is an element with the ID of the hash value. If you want the presence of the hash value to change the content of the page, then you have two options, which I outlined in my first post.

    In the future, please use code blocks (or, preferably, highlight=JavaScript blocks) for your code as it makes it easier to read.

  7. #7
    SitePoint Enthusiast Pramit's Avatar
    Join Date
    Feb 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, but I cant get anything. I am very new to this.
    I called the above function using
    <div id="shiftcontainer">
    some content
    </div>
    <a href="#called1" onclick="Javascript:ShiftChanger('shiftcontainer','called.php','called value1');">click</a>

    <a href="#called2" onclick="Javascript:ShiftChanger('shiftcontainer','called.php','called value2');">click</a>

  8. #8
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using inline events like so

    Code javascript:
    <a href="#called1" onclick="javascript:ShiftChanger('shiftcontainer','called.php','called value1');">click</a>
    <a href="#called2" onclick="javascript:ShiftChanger('shiftcontainer','called.php','called value2');">click</a>

    Do not require javascript: and also require a return value (boolean) to tell the browser whether to process the href. If you do not return false, sometimes your browser will process the href before the onclick (in this case, as its a hash, it won't.)

    We understand your new to this, and we'll try our best to aid your learning process.



Tags for this Thread

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
  •