How To Get URL, URL Parts and URL Protocol By JavaScript

Hi Friends, In this article I will explain that how we can know URL , URL parts and URL Protocol (http or https) by javascript, all programmer may be know about how to get URL and its parts by PHP , but some of them know about Javascript,So i thought to post it on my website, So it would be helpful for any one.

It is very easy to know URL parts or URL or URL protocol by javascript. No need to write so many lines of code or bunch of code, Just a simple and single line for each one. So I am explaining here one by one. for example if we have URL then below is explanation that how we can get URL parts and URl protocol by Javascript :

<script language="javascript" type="text/javascript">
alert(window.location.protocol); // this will alert protocol of link and in our example it will alert http
alert(; // this will alert host name of link and in our example it will alert hacking e
alert(window.location.pathname); // this will alert path name of link and in our example it will alert blog/example.html

So above is three ways by which you can get URL ,URL parts and URL protocol by javascript. Please make comment if you like my posts.

  • Yes I liked your post. thank you it was very helpful. JavaScript 4 Life