Obtain URL using JavaScript

Source: Internet
Author: User

The URL is the uniform resource locator (Uniform Resource Locator, URL). The complete URL consists of the following parts:

 
Scheme: // host: Port/path? Query # Fragment

 

  • Scheme: communication protocol, common HTTP, FTP, Maito, etc.
  • HOST: host, server (Computer) Domain Name System (DNS) Host Name or IP address.
  • Port: port number, which is an integer. (optional) Default port used when the scheme is omitted. For example, the default port of HTTP is 80.
  • Path: path, which is a string separated by zero or multiple "/" symbols. It is generally used to represent a directory or file address on the host.
  • Query: Query (optional) used for dynamic web pages (for example, CGI, ISAPI, PHP/JSP/asp. net and other technologies to produce web pages) transmission parameters, can have multiple parameters, separated by the "&" symbol, each parameter name and value are separated by the "=" symbol.
  • Fragment: Information fragment, a string used to specify fragments in network resources. For example, if a webpage contains multiple glossary, you can use fragment to directly locate the glossary. (Also called anchor)
  • The following example shows a URL and obtains its components.

     
    Http://www.nowamagic.net/newsDetail.php? Id = 65

     

    Window. Location. href

    You can obtain the entire URL string (the complete address bar in the browser ).

     
    1 var test = Window. Location. href; 2 alert (test );

     


       

    ProgramReturn

     
    Http://www.nowamagic.net/newsDetail.php? Id = 65

     

    Window. Location. Protocol

    Obtain the URL protocol section.

     
    1 var test = Window. Location. Protocol; 2 alert (test );

     


       

    The program returns http:

    Window. Location. Host

    Obtain the host part of the URL.

    1 var test = Window. Location. Host; 2 alert (test );

     


       

    The program returns www.nowamagic.net

    Window. Location. Port

    Obtain the URL Port

     
    VaR test = Window. Location. Port; alert (test );


       

    If the default port 80 is used (Update: Even if port 80 is added), the return value is not the default port 80, but a null character.

    Window. Location. pathname

    Obtain the URL path (that is, the file address)

     
    VaR test = Window. Location. pathname; alert (test );

     


       
    Window. Location. Search

    In addition to assigning values to dynamic languages, we can also give static pages and use JavaScript to obtain expected parameter values.

    VaR test = Window. Location. Search; alert (test );

     

       
     
    Window. Location. Hash

    Obtain the anchor.

    1 var test = Window. Location. Hash; 2 alert (test );

    Reprinted from: http://www.nowamagic.net/javascript/js_GetUrl.php


       
Related Article

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.