Web URL

What are different javascript web URL functions?

Javascript developer give lots of efforts to find different function regarding URL.

We have segregated all functions related to URL in single place for the ease of developers

 

 

Following methods are useful to retrieve URL values in javascript

Lets consider our URL is http://localhost:8080/testing/test.php


1. Retrieve http://localhost:8080/testing/test.php from URL - Use either of following

document.location.href

window.location.href

location.href

 

<script>

var values1=document.location.href;

alert(values1);

var values2=window.location.href;

alert(values2);

var values3=location.href;

alert(values3);

</script>

2. Retrieve http://localhost:8080/ from URL - Use window.location.origin

 

<script>

var values=window.location.origin;

alert(values);

</script>

3. Retrieve localhost:8080 from URL - Use location.host

 

<script>

var values=location.host;

alert(values);

</script>

4. Retrieve /testing/test.php from URL - Use window.location.pathname

 

<script>

var values=window.location.pathname;

alert(values);

</script>

5. Retrieve localhost from URL - Use window.location.hostname

 

<script>

var values=window.location.hostname;

alert(values);

</script>

6. Retrieve http: from URL - Use window.location.protocol

 

<script>

var values=window.location.protocol;

alert(values);

</script>

7. Retrieve 8080 from URL - Use location.port

 

<script>

var values=location.port;

alert(values);

</script>

8. Retrieve test.php from URL - Use either of following

window.location.pathname.replace(/^.*[\\\/]/, '');

window.location.pathname.substring(window.location.pathname.lastIndexOf('/')+1);

 

<script>

var values1=window.location.pathname.replace(/^.*[\\\/]/, '');

alert(values);

 

var values2=window.location.pathname.substring(window.location.pathname.lastIndexOf('/')+1);

alert(values2);

</script>

9. If you want to assign other URL in current window

window.location.assign("http://google.com");

 

<script>

window.location.assign("http://google.com");

//It will redirect to google.com

</script>

10. If your URL contains hash tag like http://localhost:8080/testing/test.php#divId

Retrieve #divId from URL - Use location.hash;

 

<script>

var values=location.hash;

alert(values);

</script>

11. If your URL contains query string like http://localhost:8080/testing/test.php?id=123&action=update

Retrieve ?id=123&action=update from URL - Use location.search;

 

<script>

var values=location.search;

alert(values);

</script>

Name
Email
Comments
Back
Funding

We need your support to operate it properly. We have lots of ideas but less fund, so help us with your funding.

Thoughts for the day

If a country is to be corruption free and become a nation of beautiful minds, I strongly feel there are three key societal members who can make a difference. They are the father, the mother and the teacher.

Dr. A.P.J. Abdul Kalam
Polls
Which device you use most for facebook application?
Laptop/PC
20%

 


Android
53%

 


IOS
13%

 


Blackberry
14%

 


Tutorial On Request
Q. Ask us for any tutorial or any thing which helps to build your career better.
Email:
Query: