Hoe ga ik naar een URL met jQuery?

Hoe naar een URL te gaan met jQuery of JavaScript.

<a href="javascript:void(0)"  onclick="javascript:goToURL()">Go To URL</a>
function goToURL(url){
// some code to go to url
}

Ik wil window.location niet gebruiken omdat ik deze link wil oproepen vanuit een pop-up.

Nieuwe link zou ook in een pop-up moeten openen. Ik wil Ajax ook niet gebruiken. Simuleer gewoon href in JavaScript.


Antwoord 1, autoriteit 100%

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

Antwoord 2, autoriteit 28%

waarom niet gebruiken?

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>

Antwoord 3, autoriteit 6%

window.location is precies wat je nodig hebt. Een ander ding dat u kunt doen, is een ankerelement maken en erop klikken

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

Antwoord 4

Eigenlijk moet je het anker # gebruiken om hiermee te spelen. Als u het Gmail-urlsysteem reverse-engineert, vindt u

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

Alles na # is het deel dat je op je pagina wilt laden, dan hoef je alleen maar te kiezen waar je het wilt laden.

Trouwens, als je document.location gebruikt door een #iets toe te voegen, wordt je pagina niet vernieuwd.

Other episodes