2015-07-21 81 views
3

这是代码:HTML如何在新窗口中打开此URL?

<a href="FAKE_URL" onclick="document.location.href = 'REAL_URL'; return false;"> 
<img src="IMAGE"></a> 

我已经尝试:

<a href="FAKE_URL" onclick="document.location.href = 'REAL_URL'; return false;" target="_blank"> 
<img src="IMAGE"></a> 

但不工作

+0

为什么你想要在那里的onclick事件? –

+0

[JavaScript在新窗口中打开,而不是标签]可能重复(http://stackoverflow.com/questions/726761/javascript-open-in-a-new-window-not-tab) –

+0

'' – Hearner

回答

1

试试这个:

<a href="../html-link.html" target="popup" onclick="window.open('../html-link.html','name','width=600,height=400')">Open page in new window</a> 
+0

它的工作原理! :) 谢谢 –

0

要在新窗口中打开文件,使用

onclick="window.open('yourfile.html','','width=750px,height=1000px,left=150,top=‌​0,toolbar=no,status=no,resizable=no,titlebar=no').focus();" 
1

可以使用window.open('url','_blank')的onclickevent一个新的选项卡后,打开:

<a href="https://google.com" onclick="window.open('https://youtube.com','_blank'); return false;">click here for a new tab</a> 

,或者您可以使用window.open('url','','width=,height=')打开一个新窗口:

<a href="https://google.com" onclick="window.open('https://youtube.com','','width=800,height=700'); return false;">click here for a new window...</a> 

在这个例子中它会打开youtubeREAL_URL)而不是goo GLEFAKE_URL

> JSFiddle - Example


说明:

你的错误是的target="_blank"附加改变你<a href="..."></a>的行为,但不是你的document.location.href='...'。因此,如果没有document.location.href='...',它会在新选项卡中打开FAKE_URL

相关问题