How to obtain the complete url address and parameters in javascript

Source: Internet
Author: User

Two Methods for javascript to obtain the current URL parameter:

// Return a string parameter, for example, class_id = 3 & id = 2 & function getUrlArgStr () {var q = location. search. substr (1); var qs = q. split ('&'); var argStr = ''; if (qs) {for (var I = 0; I <qs. length; I ++) {argStr + = qs [I]. substring (0, qs [I]. indexOf ('=') + '=' + qs [I]. substring (qs [I]. indexOf ('=') + 1) + '&';} return argStr ;}
// The returned Parameter function getUrlArgObject () {var args = new Object (); var query = location. search. substring (1); // obtain the query string var pairs = query. split (","); // disconnect for (var I = 0; I <pairs. length; I ++) {var pos = pairs [I]. indexOf ('='); // find name = valueif (pos =-1) {// if not found, skip continue;} var argname = pairs [I]. substring (0, pos); // extract namevar value = pairs [I]. substring (pos + 1); // extract valueargs [argname] = unescape (value); // save as attribute} return args; // return object}

In addition, it lists some functional methods for javascript to obtain each part of the url:

Window. location. host; // return the host part of the url, for example, www.xxx.com.
Window. location. hostname; // return www.xxx.com
Window. location. href; // return the entire url string (the complete address bar in the browser), for example: www.xxx.com/index.php? Class_id = 3 & id = 2
Window. location. pathname; // return/a/index. php or/index. php
Window. location. protocol; // return the url protocol, such as http:, ftp:, maito:, and so on.
Window. location. port // The port part of the url. If the default port 80 is used, the return value is not the default port 80, but an empty character.

Articles you may be interested in
  • PHP retrieves the complete url function of the current page, including Parameters
  • Windows cannot start the hardware device because its configuration information (in the Registry) is incomplete or damaged. (Code 19) Solution
  • Js modifies the value of a specified parameter in the url
  • JavaScript calculates the number of characters currently entered
  • Jquery obtains the index number of the current element in the Peer element.
  • Mysql queue for efficient Concurrent Data Reading
  • Js checks whether the image is loaded and obtains the image width.
  • JavaScript to get the number of rows and columns of a table

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.