is it possible to change change the url in address

2019-09-08 19:07发布

you all have used pinterest, you can see that when you click on any pin, a div is added and lightbox is shown on the same page but the url is changed to that of actual pin page. i have the same lightbox to show data, is it possible to change the url like that?..

one thing i want to tell is that, i have link which has onclick event which calls view() method, in which i am calling another page with ajax request, which shows the content of that page on my page, i want to change url when this link is clicked and back to previous url when closed..here is my code

function view(){
   $.get('mypage.jsp', function(html) {
   $(html).hide().appendTo('body').fadeIn(500);
   }, 'html');
};

4条回答
劫难
2楼-- · 2019-09-08 19:54

Yes, with HTML5's new history API. Where the lightbox is triggered, add something like this:

var hist = window.history;
hist.pushState({ image: [IMAGE URL] }, 'lightbox', [URL]);

This will change the current URL without reloading the page, and will create an entry in the browser history, so users can use their browser back button to return to the previous state (in this case, before they opened the lightbox).

查看更多
太酷不给撩
3楼-- · 2019-09-08 19:56

I haven't checked Pintrest's solution. But is hash what you're looking for?

window.location.hash="Whatever-you-want-to-add-to-the-URL"

This will change http://stackoverflow.com/posts/11968693/ to http://stackoverflow.com/posts/11968693/#Whatever-you-want-to-add-to-the-URL

查看更多
ゆ 、 Hurt°
4楼-- · 2019-09-08 20:07

This feature is known as HTML5 Push State. Here's a related StackOverflow question which may provide more insight. Good tutorial for using HTML5 History API (Pushstate?)

查看更多
forever°为你锁心
5楼-- · 2019-09-08 20:08

You can change the url (not only the hash) by using the pushState or replaceState functions on the history object. More details: http://diveintohtml5.info/history.html

查看更多
登录 后发表回答