JavaScript window location
In the post, you will learn about the JavaScript window location.
The window.location object can be used to get information about the current page address and to redirect the browser to a new page. The location objects contain over a dozen properties and methods. The unique window.location object has access to all of these.
Window Location Attributes
There are the some attributes of window location-
Window.location.href
The window.location.href property returns the URL (Universal Resource Locator) of the current page.
document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;
Output of the above code:
Page location is https://www.etutorialspoint.com/demo1.html
Window.location.hostname
The window.location.hostname property returns the domain name of the web host.
document.getElementById("demo").innerHTML =
"Page hostname is " + window.location.hostname;
Output of the above code:
Page hostname is www.etutorialspoint.com
Window.location.pathname
The window.location.pathname property returns the path to the given web page on its server.
document.getElementById("demo").innerHTML =
"Page pathname is " + window.location.pathname;
Output of the above code:
Page pathname is /demo1.html
Window.location.port
The window.location.port property returns the port through which a web page is served to a user.
document.getElementById("demo").innerHTML =
"Page port is " + window.location.port;
Output of the above code:
Page port is
Window.location.protocol
The window.location.protocol property returns the web protocol a web page uses to transfer its information.
document.getElementById("demo").innerHTML =
"Page port is " + window.location.protocol;
Output of the above code:
Page port is https:
Window.location.assign
The window.location.assign property is used to navigate the browser to a new web page. It works much like the property window.location.href.
document.getElementById("demo").innerHTML =
"Page port is " + window.location.assign;
Output of the above code:
Page port is function assign() { [native code] }
Related Articles
Generate random numbers in JavaScriptScientific calculator JavaScript
Simple calculator using JavaScript
Palindrome number in JavaScript
Remove duplicates from array Javascript
How to reverse string in Javascript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh