Knowledge Walls
John Peter
Pune, Maharashtra, India
How to get current web page url, hostname, port, protocol And URI in Javascript
6374 Views
Window.location.href 
window.location.href returns current web page browser url. URL contains all parts of url which means protocol, hostname, port and pathname.

Example
http://www.google.com:8054/mail

http - protocol
www.google.com - hostname
8054 - port
/mail - pathname
Window.location.protocol 
window.location.protocol returns http or https protocol of the current web page request in javascript.
Window.location.hostname 
window.location.hostname returns current web page browser hostname of the url.
Window.location.port 
window.location.port returns port number of the url if it is not exist so returns empty.
Window.location.pathname 
window.location.pathname returns current web page URI.
Example
document.write("URL: "+window.location.href);
document.write("<br />");

document.write("PROTOCOL: "+window.location.protocol);
document.write("<br />");

document.write("HOST NAME: "+window.location.hostname);
document.write("<br />");

document.write("PORT: "+window.location.port);
document.write("<br />");

document.write("WEB PAGE URI: "+window.location.pathname);
document.write("<br />");
Demo & Output 
Best Lessons of "Good Javascript Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details