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

14.2.1 Parsing URLs

The Document object also has a URL property, which is a static string that holds the URL of the document when it was first loaded. If you navigate to fragment identifiers (like “#table-of-contents”) within the document, the Location object is updated to reflect this, but the document.URL property remains unchanged.

14.2.1 Parsing URLs

The location property of a window is a reference to a Location object; it represents the current URL of the document being displayed in that window. The href property of the Location object is a string that contains the complete text of the URL. The toString() method of the Location object returns the value of the href property, so in contexts that will implicitly invoke toString(), you can just write location rather than location.href.

Other properties of this object—protocol, host, hostname, port, pathname, search, and hash—specify the various individual parts of the URL. They are known as “URL decomposition” properties, and they are also supported by Link objects (created by andelements in HTML documents). See the Location and Link entries in Part IV for further details.

The hash and search properties of the Location object are interesting ones. The hash property returns the “fragment identifier” portion of the URL, if there is one: a hash mark (#) followed by an element ID. The search property is similar. It returns the portion of the URL that starts with a question mark: often some sort of query string. In general, this portion of a URL is used to parameterize the URL and provides a way to embed arguments in it. While these arguments are usually intended for scripts run on a server, there is no reason why they cannot also be used in JavaScript-enabled pages. Example 14-2 shows the definition of a general-purpose urlArgs() function you can use to extract arguments from the search property of a URL. The example uses decodeURIComponent(), which is a global function defined by client-side JavaScript. (See Global in Part III for details.)

14.2 Browser Location and Navigation | 343

Example 14-2. Extracting arguments from the search string of a URL


*This function parses ampersand-separated name=value argument pairs from*the query string of the URL. It stores the name=value pairs in*properties of an object and returns that object. Use it like this: **var args = urlArgs(); // Parse args from URL*var q = args.q || ""; // Use argument, if defined, or a default value*var n = args.n ? parseInt(args.n) : 10; */

function urlArgs() { var args = {}; // Start with an empty object var query =; // Get query string, minus '?' var pairs = query.split("&"); // Split at ampersands for(var i = 0; i < pairs.length; i++) { // For each fragment

var pos = pairs[i].indexOf('='); // Look for "name=value" if (pos == -1) continue; // If not found, skip it var name = pairs[i].substring(0,pos); // Extract the name var value = pairs[i].substring(pos+1); // Extract the value value = decodeURIComponent(value); // Decode the value args[name] = value; // Store as a property

} return args; // Return the parsed arguments }

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