如何使用jQuery或JavaScript访问一个URL。

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

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

}

我不想用window。位置,因为我想调用这个链接从一个弹出。

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


当前回答

为什么不使用呢?

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

<!DOCTYPE html > < html > < >头 <脚本> 函数goToURL() { 的位置。Href = 'http://google.it'; } > < /脚本 < / >头 <身体> <a href="javascript:void(0)" onclick="goToURL();返回错误;>到URL</a> < /身体> < / html >

其他回答

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

实际上,你必须使用锚#来处理这个。如果你逆向设计Gmail url系统,你会发现

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

#之后的所有内容都是你想在页面中加载的部分,然后你只需要选择在哪里加载它。

顺便说一下,使用文档。通过添加#something不会刷新您的页面。

为什么不使用呢?

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

<!DOCTYPE html > < html > < >头 <脚本> 函数goToURL() { 的位置。Href = 'http://google.it'; } > < /脚本 < / >头 <身体> <a href="javascript:void(0)" onclick="goToURL();返回错误;>到URL</a> < /身体> < / html >

窗口。位置正是你所需要的。你可以做的另一件事是创建锚元素并模拟点击它

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