如何使用 jQuery 访问 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16959476/
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 18:09:01  来源:igfitidea点击:

How to go to a URL using jQuery?

javascriptjquery

提问by user1990525

How to go to a URL using jQuery or JavaScript.

如何使用 jQuery 或 JavaScript 访问 URL。

<a href="javascript:void(0)"  onclick="javascript:goToURL()">Go To URL</a>

function goToURL(url){
// some code to go to url

}

I don't want to use window.location as I want to invoke this link from a popup.

我不想使用 window.location 因为我想从弹出窗口调用这个链接。

New link should also open in a popup. I also don't want to use Ajax. Just simulate href in JavaScript.

新链接也应该在弹出窗口中打开。我也不想使用 Ajax。只需在 JavaScript 中模拟 href。

回答by Alvaro

//As an HTTP redirect (back button will not work )
window.location.replace("http://www.google.com");

//like if you click on a link (it will be saved in the session history, 
//so the back button will work as expected)
window.location.href = "http://www.google.com";

回答by user2459202

why not using?

为什么不使用?

location.href='http://www.example.com';

<!DOCTYPE html>
<html>

<head>
  <script>
    function goToURL() {
      location.href = 'http://google.it';

    }
  </script>
</head>

<body>
  <a href="javascript:void(0)" onclick="goToURL(); return false;">Go To URL</a>
</body>

</html>

回答by Chris Panayotoff

window.location is just what you need. Other thing you can do is to create anchor element and simulate click on it

window.location 正是您所需要的。您可以做的另一件事是创建锚元素并模拟点击它

$("<a href='your url'></a>").click(); 

回答by MaximeBernard

Actually, you have to use the anchor # to play with this. If you reverse engineer the Gmail url system, you'll find

实际上,你必须使用锚#来玩这个。如果您对 Gmail 网址系统进行逆向工程,您会发现

https://mail.google.com/mail/u/0/#inbox
https://mail.google.com/mail/u/0/#inbox?compose=new

Everything after # is the part your want to load in your page, then you just have to chose where to load it.

# 之后的所有内容都是您要加载到页面中的部分,然后您只需选择加载它的位置。

By the way, using document.location by adding a #something won't refresh your page.

顺便说一句,通过添加 #something 来使用 document.location 不会刷新您的页面。