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



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 http://www.hackingethics.com/example.html 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 http://www.hackingethics.com/example.html it will alert http
alert(window.location.host); // this will alert host name of link and in our example http://www.hackingethics.com/example.html it will alert hacking e thics.com
alert(window.location.pathname); // this will alert path name of link and in our example http://www.hackingethics.com/example.html it will alert blog/example.html
</script>

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.

  • http://www.facebook.com/people/Eddie-Butler/514330856 Eddie Butler

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