2015-07-21 80 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

相關問題