Home  >  Article  >  Web Front-end  >  How to get current URL in web browser using JavaScript

How to get current URL in web browser using JavaScript

不言
不言Original
2019-02-15 16:32:283259browse

How to get current URL in web browser using JavaScript provides many methods to display the current URL in the web browser address bar. You can obtain these details using the Location object property of the Window object. Below is a list of some properties of the Location object. The first example will get the current URL in a web browser. Some examples are also listed below for reference. Let’s look at the specific content.

How to get current URL in web browser using JavaScript

1. href: Returns the entire URL displayed in the address bar.

var currentURL = window.location.href;

2. host: Returns the host name and port of the URL in the address bar.

var currentHost = window.location.host;

3. hostname: Only the host name of the URL in the address bar is returned.

var currentHostname = window.location.hostname;

4. post: Only return the port details of the URL in the address bar.

var currentPort = window.location.port;

5. protocol: Returns the protocol of the URL in the address bar. Just like the URL uses HTTP (without SSL) or HTTPS (with SSL).

var currentProtocol = window.location.protocol;

6. pathname: Returns the path name of the URL in the address bar (the value after the domain name).

var currentPathname = window.location.pathname;

7. pathname: Returns the anchor part of the URL, including hash singh (#).

var currenthash = window.location.hash;

8. search: Returns the query part of the URL, such as the part starting with a question mark (?).

var currentSearchString = window.location.search;

This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the php Chinese website! ! !

The above is the detailed content of How to get current URL in web browser using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn