Wednesday, September 9, 2009

How to get the URL parts using Javascript

Hi,
Here is the code to get the URL parts using Javascript.
1) How to get the path name
var pathname= window.location.pathname;
alert( " Pathname "+ pathname );
2) How to get the hostname
var hostname = window.location.host;
alert( hostname);
3) How to get the complete URL
var URL = window.location;
alert("URL "+URL);
4) How to get the URL with out parameters
var URLWithOutPrameters = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
alert(URLWithOutPrameters );
5) How to get the Protocal in the URL.
var protocal = window.location.protocol;
alert( " protocal "+ protocal);
5) How to get the parameters in the URL
var name = "tagName"; // We can place any name , the name should contain in the URL.
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results != null )
alert( results[1]); // will display the value of the name (key value pair) ,
}

No comments: