How do I get the URL
current one on the website? using javascript
.
What I want is to know if I can get the url
one from a website, but for example if I am in the section I /conocenos
want to get http://sitioWeb/conocenos
.
How do I get the URL
current one on the website? using javascript
.
What I want is to know if I can get the url
one from a website, but for example if I am in the section I /conocenos
want to get http://sitioWeb/conocenos
.
in
javascript
, you can get the url with: location.hrefor window.location
In summary you can use:
window.location.href : Gets the href (url) of the current page.
Javascript provides many methods to get 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 so:
either
either
Behavior may depend on browser
Basic structure of a URL:
1.- Protocol -- Specifies the name of the protocol to use 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 that the web client wants to access, for example /index.html
5.- query -- Follows the path of the component and provides a string of information that the resource can use for the same purpose (eg, 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 hash identifier
host - Get or set the hostname and port of a URL
hostname - Gets or sets the hostname of a URL
href - Get or set the entire URL
pathname - Gets or sets the pathname of a URL
port - Gets or sets the port number that the server uses for the URL
protocol - Gets or sets the protocol of a URL
search - Gets or sets the query part of a URL
Retrieved from: https://stackoverflow.com/a/20746566/3596441
See also
https://developer.mozilla.org/en-US/docs/Web/API/Document/location http://www.comptechdoc.org/independent/web/cgi/javamanual/javalocation.html
If using
window.location
, which is synonymous withlocation.href
Note: this example does not show the stack overflow address since the stack snippets are hosted on another domain.
I know there are good answers already, but there's an unforeseen case that might be interesting: what happens if you're inside a
iframe
? Do you expect the iframe URL or the one displayed in the browser?If what you want is the URL indicated in the
src
deliframe
, the method indicated by others will work:But if what you want is the URL that is displayed in the browser's address bar, in that case you have to read the data from the parent:
One method to get the URL that appears in the browser would be to do something like this:
In that case you will get the address of the page if it is not included in another one, and if it is (eg through an iframe), then you will get the address of the parent.