如何获取/设置当前页面的URL(跨时空浏览器版本运行)

如何获取/设置当前页面的URL(跨时空浏览器版本运行)

问题描述:

我想在发生某些事件时获取/设置当前页面的网址.

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.

使用Jquery

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

使用JavaScript

Using JavaScript

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

哪种方法适用于时空浏览器版本?

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

获取JavaScript中的当前URL?

如何重定向到另一个JavaScript/jQuery中的网页?

我认为不需要为此使用jQuery.以下内容将在所有浏览器中正常运行:

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";

或更简单地:

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