Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get/set current page URL (which works across space-time-browsers-versions)

I want to get/set URL of the current page upon certain event.

It seems there are more than one approach for doing it as mentioned in questions/answers below.

Using Jquery

Get URL - $(location).attr('href'); Set URL - $(location).attr('href',url); 

Using JavaScript

Get URL - myVar = window.location.href; Set URL - window.location.href = "http://stackoverflow.com"; 

Which approach works across space-time-browsers-versions?

Get current URL in JavaScript?

How to redirect to another webpage in JavaScript/jQuery?

like image 376
Ganesh Bhosle Avatar asked Aug 23 '13 06:08

Ganesh Bhosle


People also ask

How do I get the current URL in my browser?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.

How do I change the current URL in JavaScript?

You can use the window. location property to access the URL of the current page . If you want to go to a new page , either you can change the href property of history object or call assign method with new url as argument.

How do I change URL without reloading?

There is no way to modify the URL in the browser without reloading the page. The URL represents what the last loaded page was. If you change it ( document. location ) then it will reload the page.

How do you change a Web page using JavaScript?

open() to Change Page in JavaScript. The window. open() is a window interface method provided by JavaScript, which loads the specified URL/resource into a new tab or an existing browser with the specified name. This method will generate a new window to open a specified URL.


1 Answers

I don't see any need to use jQuery for this. The following will work perfectly fine in all browsers:

window.location.href = "http://stackoverflow.com"; 

Or even more simply:

location.href = "http://stackoverflow.com"; 
like image 180
meub Avatar answered Sep 19 '22 06:09

meub