In the Javascript to get current window properties we will use the Window.location. With this object we can fetch properties like host name, path, protocol etc
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Style-Type" content="text/css"> <title></title> <meta name="Generator" content="Cocoa HTML Writer"> </head> <body> <h2>Get URL for the Current web page/window</h2> <p id="id_url"></p> <script> document.getElementById("id_url").innerHTML = "The full URL of this page is:<br>" + window.location.href; console.log( window.location.href); </script> </body> </html> |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Style-Type" content="text/css"> <title></title> <meta name="Generator" content="Cocoa HTML Writer"> </head> <body> <h2>Get URL for the Current web page/window</h2> <p id="id_url"></p> <script> var data="The full URL of this page is:<br>" + window.location.href; data=data+ "<br> <br> Host name of the Page : " +window.location.hostname; data=data+ "<br> <br>Path of the Page : " +window.location.pathname; data=data+ "<br> <br>Port of the page : " +window.location.port; document.getElementById("id_url").innerHTML =data; console.log( window.location.href); </script> </body> </html> |
By using window.location.assign() property we can load new page.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Style-Type" content="text/css"> <title></title> <meta name="Generator" content="Cocoa HTML Writer"> </head> <body> <h2>Get URL for the Current web page/window</h2> <p id="id_url"></p> <input type="button" value="Load new page" onclick="newPage()"> <script> var data="The full URL of this page is:<br>" + window.location.href; data=data+ "<br> <br> Host name of the Page : " +window.location.hostname; data=data+ "<br> <br>Path of the Page : " +window.location.pathname; data=data+ "<br> <br>Port of the page : " +window.location.port; document.getElementById("id_url").innerHTML =data; console.log( window.location.href); function newPage() { window.location.assign("https://rrtutors.com") } </script> </body> </html> |
Conclusion: To get current window/web page information we will window.location object which will returns the hostname, path, port number details
Article Contributed By :
|
|
|
|
506 Views |