JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

14.2.2 Loading New Documents

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 = ""; // 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):

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1