HTML5 's Onhashchange and history management

Source: Internet
Author: User

HTML5 's Onhashchange and history management

Now the development, more and more inclined to jump in the page, in this case need to update their knowledge, to achieve the page jump. History is the solution to this problem.

There are two ways to HTML5 the solution:

1,onhashchange
Use of Window.loaction.hash object (save, Fetch)


2,history

(1) pushstate three parameters: data, title (empty), url (optional).
(2) Popstate is an event that reads Event.state data
Note: The URL is false. The user cannot actually find it.


Essentially: Both methods are stored value + value events. After simplification, it is:
Window.loaction.hash = Srcarr;
Window.onhashchange=function () {
var val = Window.loaction.hash;
}
And
History.pushstate = Srcarr;
Window.onpopstate=function (event) {
var val = event.state;
};


For details, refer to the following random number examples:

HTML code
    1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
    2. <HTML xmlns="http://www.w3.org/1999/xhtml">
    3. <head>
    4. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
    5. <title> Untitled document </title>
    6. <script type="Text/javascript">
    7. window.onload=function () {
    8. var oinput = document.getElementById ("input1");
    9. oinput.onclick=function () {
    10. var odiv = document.getElementById ("Div1");
    11. var num = generatenum (6);
    12. Onhashchange
    13. window.location.hash = num;
    14. Window.onhashchange = function () {
    15. odiv.innerhtml = window.location.hash.substring (1);
    16. }
    17. History
    18. /*
    19. History.pushstate (num, ");
    20. window.onpopstate = Function (event) {
    21. odiv.innerhtml=event.state;
    22. }*/
    23. odiv.innerhtml=num;
    24. Generate random numbers
    25. function Generatenum (num) {
    26. var ret = [];
    27. for (var i = 0; I < num; i++) {
    28. Ret.push (Math.ceil (100*math.random ()));
    29. };
    30. return ret;
    31. }
    32. }
    33. }
    34. </Script>
    35. </head>
    36. <body>
    37. Winning number:
    38. <div id="Div1"></div>
    39. <input type="button" value="Generate random Number" id="input1" />
    40. </body>
    41. </HTML>

HTML5 's Onhashchange and history management

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.