Adsense

Get URL, URL part, URL parameters using javascript



Getting URL or URL parts through javascript is so easy. We can get protocal using this code :
  1. var proto    = window.location.protocol;  
  2. alert( proto );  
this code return "http:" protocol.
If you want to get host name from the URL using javascript then you can simple write this code:

  1. var hostName = window.location.host;  
  2. alert( hostName );  
this code return the host name like if you are running this code in local host then it returns "localhost"
if you want to get path of the URL with javascript then use this:

  1. var pathName = window.location.pathname;  
  2. alert( pathName );  
this code returns the path name of the current running script like you are running the script in test.html the it return the full path name with filename test.html
Or if you want to get parameter used in URL with javascript then simply do this:

  1. var params   = window.location.search;  
  2. alert( params );  
it returns the parameter used in the URL

now lets join all the code :

  1. var proto    = window.location.protocol;  
  2. var hostName = window.location.host;  
  3. var pathName = window.location.pathname;  
  4. var params   = window.location.search;  
  5.   
  6. alert( proto + hostName + pathName + params );  

newest questions on wordpress