Get URL where I'm located with JavaScript

9

How do I get the URL current on the website? using javascript .

What I want is to know if I can get the url of a website, but for example if I'm in the /conocenos section you want to get http://sitioWeb/conocenos .

    
asked by José Gregorio Calderón 02.02.2016 в 18:25
source

4 answers

10

in javascript , you can get the url with: location.href

<script type="text/javascript">
alert(location.href);
</script>

or window.location

   <script type="text/javascript">
   alert(window.location);
    </script>

In summary you can use:

window.location.href : get the href (url) of the current page.

    
answered by 02.02.2016 / 18:28
source
6

Javascript provides many methods to obtain and change the current URL, these methods use the Location object which is a property of the Window object, you can create a new Location object that has the current URL like this:

var LocationActual=window.location;

or

window.location.href 

or

document.URL;

Behavior may depend on the browser

Basic structure of a URL:

<protocol>//<hostname>:<port>/<pathname><search><hash>

1.- Protocol - Specifies the name of the protocol to be used to access the internet resource (HTTP without SSL or HTTPS with SSL)

2.- hostname - Specifies the host that owns the resource, for example www.stackoverflow. A server provides services using the host name.

3.- port - A port number used to recognize a specific process to which a message from a network or internet is redirected when it arrives at the server.

4.- pathname - Provides information about the specific resource within the host to which the web client wants to access, for example /index.html

5.- query - Follow the path of the component and provide a chain of information that the resource can use for the same purpose (example, search parameters or data to process)

6.- hash - Fragment identifier, includes the symbol (#)

With these properties you can access all these components of the URL

  • hash - Gets or sets the fragment identifier

  • host - Get or set the hostname and port of a URL

  • hostname - Get or set the hostname of a URL

  • href - Get or set the entire URL

  • pathname - Get or set the path name of a URL

  • port - Gets or sets the port number that the server uses for the URL

  • protocol - Get or set the protocol of a URL

  • search - Gets or sets the query part (or query) of a URL

  • Retrieved from: link

    See also

    link link

        
    answered by 02.02.2016 в 18:48
    4

    If using window.location , which is synonymous with location.href

    alert(window.location);

    Note: this example does not show the stack overflow address since stack snippets host in another domain.

        
    answered by 02.02.2016 в 18:27
    3

    I know that there are already good answers, but there is an unforeseen case that could be interesting: what happens if you are within iframe ? Do you expect the URL of the iframe or the one shown in the browser?

    If you want the URL indicated in src of iframe , the method indicated by others will work:

    var url = window.location.href;
    

    But if what you want is the URL that is displayed in the address bar of the browser, in that case you have to read the data of the father:

    var url = window.parent.location.href;
    

    One method to get the URL that appears in the browser would be to do something like this:

    var url = (window.location != window.parent.location) ?
                              window.parent.location.href : window.location.href;
    

    In that case you will get the address of the page if it is not included in another, and if it is (eg through an iframe), then you will get the address of the father.

        
    answered by 04.02.2016 в 16:16