如何去使用jQuery的URL?

如何去使用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.location,因为我想从popup窗口中调用此链接。

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

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

为什么不使用?

 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> 

window.location正是你所需要的。 你可以做的其他事情是创build锚点元素并模拟点击它

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

事实上,你必须使用锚点#来玩这个。 如果您对Gmailurl系统进行反向工程,您会发现

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

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

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