Use JavaScript to modify the implementation code of the browser URL address bar

Source: Internet
Author: User

From http://www.jb51.net/article/42240.htm

Now in the browser, there is a very interesting feature, you can not refresh the page without the situation to modify the browser URL; During the browsing process. You can store your browsing history, and when you click the Back button in your browser, you can go through history to get back information, which doesn't sound complicated and can be done, so let's write some code. Let's see how it works.

<Scripttype= "Text/javascript">                    functionJusturlchange () { for(i=0; I<5; I++){                      varJ=I+Ten; varStateObject={id:j};//JSON format                      vartitle= "Wow Title"+i; varNewurl= "/springmvc/teacher/inwhichroom.do?id="+i;                      History.pushstate (Stateobject,title,newurl); /*The first parameter, is a JSON object, where you store any historical information about the current URL.                      The second argument, title is equivalent to passing the title of a document. The third parameter is used to pass a new URL. You will see that the browser's address bar has changed and the current page has not been refreshed. */                }            }                        /*now run, click the browser's Back button to see how the URL is changed.            For each URL change, it is because it stores the historical status "id" and the corresponding value.            But how do we regain the state of history and do something on that basis? We need to add an event listener to "Popstate", which will be triggered every time the state of the historical object changes. */Window.addeventlistener ('popstate', function(event) {//add a "back" listener event to the browserReadState (event.state);//all the information that pops up is J.            }); /*Now you will see that whenever you click the Back button, a popstate event will be triggered. Our event listener then retrieves the URL of the historical state object associated with it and prompts for the value of the ID*/            functionReadState (data) {alert (data.id); } </Script>

Use JavaScript to modify the implementation code of the browser URL address bar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.