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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18396501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:32:10  来源:igfitidea点击:

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

javascriptjquerydom

提问by Ganesh Bhosle

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

我想在特定事件中获取/设置当前页面的 URL。

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

正如下面的问题/答案中提到的那样,似乎有不止一种方法可以做到这一点。

Using Jquery

使用 Jquery

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

Using JavaScript

使用 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?

在 JavaScript 中获取当前 URL?

How to redirect to another webpage in JavaScript/jQuery?

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

回答by meub

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

我认为没有必要为此使用 jQuery。以下将在所有浏览器中完美运行:

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

Or even more simply:

或者更简单:

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

回答by Jinxed

I agree with @meub . this will also do :

我同意@meub。这也可以:

window.location.replace("http://stackoverflow.com");

To behave as clicking a link you need to use simple javascript . You don't need jQuery for doing that. You can use the following:

要表现得像单击链接一样,您需要使用简单的 javascript 。你不需要jQuery来做到这一点。您可以使用以下内容:

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