14.2.2 Loading New Documents
The assign() method of the Location object makes the window load and display the document at the URL you specify. The replace() method is similar, but it removes the current document from the browsing history before loading the new document. When a script unconditionally loads a new document, the replace() method is often a better choice than assign(). Otherwise, the Back button would take the browser back to the original document, and the same script would again load the new document. You might use location.replace()to load a static HTML version of your web page if you detected that the user’s browser did not have the features required to display the full-featured version:
// If the browser does not support the XMLHttpRequest object // redirect to a static page that does not require it. if (!XMLHttpRequest) location.replace("staticpage.html");
Notice that the URL passed to replace()is a relative one. Relative URLs are interpreted relative to the page in which they appear, just as they would be if they were used in a hyperlink.
In addition to the assign() and replace() methods, the Location object also defines reload(), which makes the browser reload the document.
A more traditional way to make the browser navigate to a new page is to simply assign the new URL directly to the location property:
location = "http://www.oreilly.com"; // Go buy some books!
You can also assign relative URLs to location. They are resolved against the current URL:
location = "page2.html"; // Load the next page
A bare fragment identifier is a special kind of relative URL that does not cause the browser to load a new document but simply scroll to display a new section of the document. The identifier #top is a special case: if no document element has the ID “top”, it makes the browser jump to the start of the document:
location = "#top"; // Jump to the top of the document
The URL decomposition properties of the Location object are writable, and setting them changes the location URL and also causes the browser to load a new document (or, in the case of the hash property, to navigate within the current document):欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31420.html