2013-06-06 4 views
152

jQuery 또는 JavaScript를 사용하여 URL로 이동하는 방법.jQuery를 사용하여 URL로 이동하는 방법?

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

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

} 

나는 팝업에서이 링크를 호출하려고하므로 window.location을 사용하고 싶지 않습니다.

새 링크도 팝업으로 열어야합니다. 나는 또한 Ajax를 사용하고 싶지 않다. JavaScript에서 href를 시뮬레이션하면됩니다.

+0

"window.location은 팝업입니다." –

+2

** ** document.location.href ** 대신 window.location.replace **. 여기를 확인하십시오, http : //stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript – dreamweiver

+1

방금 ​​답변을 검색해야합니다. 이것은 매우 일반적인 질문입니다. . – GEMI

답변

13

window.location은 필요한 것입니다. 당신이 할 수있는 다른 일은 앵커 요소를 생성하고 클릭을 시뮬레이트하는 것입니다.

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

하하 흥미 롭습니다.하지만이 솔루션을 사용할 경우가 있습니까? – AlbertSamuel

+1

상대 링크 어쩌면? – beppe9000

+1

새 탭에서'target = "_ blank"' –

3

<!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>
location.href='http://www.example.com'; 

는 사실이 놀 앵커 번호를 사용해야합니다. 당신은 Gmail의 URL이 시스템을 리버스 엔지니어링 경우 # 당신의 희망이 페이지를로드 할 수있는 부분입니다 후, 당신은 그 때 당신은 어디를로드하는 선택해야

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

모든 것을 찾을 수 있습니다.

그런데 document.location에 #something을 추가하면 페이지가 새로 고쳐지지 않습니다.

254
//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"; 
+2

을 추가 하시겠습니까? – Deekor

+0

'window.location.href'와'location.href'의 차이점 –

+1

@YahyaUddin 차이점이 없습니다. window.location은 location과 완전히 똑같은 객체입니다. 원인의 다른 곳에서 덮어 쓰지 않는 한. –

관련 문제