JavaScript window location; Through this tutorial, i am going to show you what is javascript window location features and how to use it.
The window.location object can be used to get the current webpage (URL) and to redirect the browser to a new webpage. The window prefix is not necessary but in some cases we need it.
JavaScript Window location Methods
- window.location.href => get the current webpage.
- window.location.hostname => get the webhost (domain name).
- window.location.pathname => get filename path of the current webpage.
- window.location.protocol => get the web protocol used (http: or https:)
- window.location.assign => loads a new document.
1: The Window location href
The window.location.href method return the current webpage URL.
var exp = window.location.href; document.write("Current url of browser = "+exp);
2: Window Location Hostname
The window.location.hostname method used to which is return the name of the current pages internet host.
var exp = window.location.hostname; document.write("hostname = "+exp);
var exp = window.location.hostname; document.write("hostname = "+exp);
3: Window Location Pathname
The window.location.pathname method is used for get the pathname of the current webpage.
var exp = window.location.pathname; document.write("Pathname = "+exp);
4: Window Location Protocol
The window.location.protocol method is used for get the protocol of the current webpage.
var exp = window.location.protocol; document.write("Protocol = "+exp);
5: Window Location Assign
The window.location.assign() method loads a new document.
function newDocument() { window.location.assign("https://www.Laratutorials.com/") }
More JavaScript Tutorials
Recommended:-JavaScript LocalStorage
Recommended:-JavaScript IP Address Validation Example
Recommended:-Highlight Searched text on a page with in Javascript
Recommended:-JavaScript Encode and Decode URL
Recommended:-Check if variable is a number in JavaScript
Recommended:-JavaScript Convert a String to Integer Number
Recommended:-Resize Image using Javascript
Recommended:-JavaScript Call Function After Whole Page Load
Recommended:-Compare two dates with JavaScript
Recommended:-JavaScript Arrow Function es6 Example
Recommended:-JavaScript Promise with Example
Recommended:-JavaScript: Object Properties
Recommended:-20+ JavaScript Array Methods
Recommended:-20 + JavaScript String Methods
Recommended:-JavaScript Clone Array Method with Example
Recommended:-JavaScript Remove First, Last Element From Array
Recommended:-JavaScript Remove Duplicate From Array of Object
Recommended:-Extract Elements From Array in JavaScript
Recommended:-JavaScript Convert Array to Comma Separated String
Recommended:-JavaScript Convert JSON String to JSON Object
Recommended:-JavaScript Convert JSON Object to Object String