开发者

AJAX unique URL [duplicate]

开发者 https://www.devze.com 2023-02-15 18:06 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How does facebook rewrite the source URL of a page in the browser address bar?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How does facebook rewrite the source URL of a page in the browser address bar?

Maybe this is a silly question, and I apologize if it's a simple one, but I'm not sure i开发者_如何学Pythonf I'm getting the terminology correct so I haven't been able to find any results with google.

I'm looking to change/modify the current page's URL (or specifically it's GET variables) using AJAX. I didn't think this was at all possible but it appears to be done in Facebook.

For instance when I'm on my facebook profile the url is written:

http://www.facebook.com/profile.php?id=xxxxx&sk=wall

Then if I click on the info link below my profile picture it changes to:

http://www.facebook.com/profile.php?id=xxxxx&sk=info

And there is no page refresh (as far as I can see).

So what's the deal, how is this done?


AFAIK you cannot change the URL without a refresh except to add a hashtag anchor. You can use the location.replace method:

http://www.w3schools.com/jsref/met_loc_replace.asp

Updated to reflect @Crescent Fresh's link

Looks like there is another option with HTML 5's history.pushState() (currently only supported by Webkit):

How does facebook rewrite the source URL of a page in the browser address bar?


The page URL query-string cannot be modified without a new request, but its "fragment identifier" (the part after the #) can, with javascript (thus Ajax).

You can even leverage that using ScriptManager's EnableHistory property.

<asp:ScriptManager ID="ScriptManager1" runat="server" EnableHistory="true" onnavigate="ScriptManager1_Navigate" />

and

protected void Button_Click(object sender, EventArgs e)
{
   //Do something.
   // ...

   //Then "remember the state".
   ScriptManager1.AddHistoryPoint("SomeState", "SomeStateValue"); 
}

protected void ScriptManager1_Navigate(object sender, HistoryEventArgs e)
{
  //Get the state back.
  String statevalue = e.State["SomeState"];
  // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消