Location is JavaScript inside the management address bar built-in objects, such as location.href to manage the URL of the page, with Location.href=url can directly redirect the page URL. Location.hash can be used to get or set the label value of a page. For example, Http://domain/'s location.hash= "#admin". Using this attribute value can do a very meaningful thing.
Window.location.hash Simple Application
First, #的涵义
#代表网页中的一个位置. The character to its right is the identifier for that position. Like what
Http://www.example.com/index.html
Represents the print location of the page index.html. When the browser reads this URL, the print location is automatically scrolled to the viewable area.
There are two methods for specifying identifiers for Web page locations. One is to use anchor points, such as <a name= "print" ></a>, and the second is to use ID attributes, such as <div id= "print" >.
Second, HTTP requests do not include #
#是用来指导浏览器动作的, completely useless on the server side. Therefore, the # is not included in the HTTP request.
For example, to visit the URL below,
Http://www.example.com/index.html
The actual request made by the browser is this:
Get/index.html http/1.1
Host:www.example.com
Can see, just request index.html, there is no "#print" part.
Third, #后的字符
Any character that appears after the first # will be interpreted by the browser as the location identifier. This means that none of these characters will be sent to the server side.
For example, the following URL is intended to specify a color value:
Http://www.example.com/?color= #fff
However, the actual request made by the browser is:
Get/?color= http/1.1
Host:www.example.com
As you can see, "#fff" has been omitted. Only the # transcoding is%23, and the browser will treat it as a literal character. In other words, the URL above should be written:
Http://example.com/?color=%23fff
Iv. Change # do not trigger Web page overload
Simply changing the part of the #, the browser scrolls only to the appropriate location and does not reload the page.
For example, from
Http://www.example.com/index.html#location1
Change into
Http://www.example.com/index.html#location2
The browser does not request index.html from the server again.
Change the history of browser access
Every time you change the part, you add a record to the browser's access history and use the back button to go back to the previous position.
This is especially useful for AJAX applications, which can represent different access states with different # values, and then give the user a link that can access a state.
It is noteworthy that the above rules are not valid for IE 6 and IE 7, and they will not increase the history of the change.
Six, Window.location.hash read # value
Window.location.hash This property can be read and writable. When read, it can be used to determine whether the state of the Web page changes, and when written, it creates an access history without overloading the Web page.
Vii. Onhashchange Events
This is an additional event in HTML 5 that triggers this event when the # value is changed. ie8+, Firefox 3.6+, Chrome 5+, and Safari 4.0+ support this event.
There are three ways to use it:
Window.onhashchange = func;
<body onhashchange= "func ();" >
Window.addeventlistener ("Hashchange", Func, false);
For browsers that do not support onhashchange, you can monitor Location.hash changes with setinterval.
Eight, Google crawl # mechanism
By default, Google's web spider ignores the # part of the URL.
However, Google also stipulates that if you want AJAX-generated content to be read by the browse engine, then "#!" can be used in the URL, and Google will automatically turn its subsequent content to the value of the query string _escaped_fragment_.
For example, Google found that the URL for the new Twitter version is as follows:
http://twitter.com/
You will automatically crawl another URL:
Http://twitter.com/?_escaped_fragment_=/username
With this mechanism, Google can index dynamic AJAX content.
We come to see a netizen to encounter such a problem, as shown in the following code, each click of the button on the page will change the value in the browser's address bar so that it can be spoofed to the browser (and not send a new request to the server) so that its "back" and "forward" buttons are available.
Now the problem is, in the ie6+ and FF3 the value of the browser has changed, but only in the FF3 under the "Forward" and "back" can be used, under IE these two are gray is not available, may I ask why?
The code is as follows
The following is about the use of hash combined with Ajax, Ajax every time the page is updated after updating the browser does not produce a history, that is, back and forward button lost the utility of the application, this can be combined with hash and window.onhashchange to use, pay attention to IE6, 7 Do not support Onhashchange, but you can use setinterval regularly check the change of the hash, or the OnLoad check method.
Specific implementation:
<body>
<div id= "Div1" ></div>
<input type= "button" value= "click" onclick= "Gett ()"/>
</body>