2012-01-18 66 views
6

如何使用其id在新窗口中打開圖像?Open image in new window

function swipe() 
{ 
    var largeImage = document.getElementById('largeImage'); 
    largeImage.style.display = 'block'; 
    largeImage.style.width=200+"px"; 
    largeImage.style.height=200+"px";     
} 

該功能在點擊圖像時被調用。現在,它在同一個窗口中打開,但我想在新窗口中打開它。這如何實現?

回答

17
function swipe() { 
    var largeImage = document.getElementById('largeImage'); 
    largeImage.style.display = 'block'; 
    largeImage.style.width=200+"px"; 
    largeImage.style.height=200+"px"; 
    var url=largeImage.getAttribute('src'); 
    window.open(url,'Image','width=largeImage.stylewidth,height=largeImage.style.height,resizable=1'); 
} 

HTML代碼:

<img src="abc.jpg" onClick="swipe();"/> 
+0

完美。非常感謝:) – Khush

7

對於有丟失的主窗口後面一個很好的機會,一個新的窗口,一般惱人的用戶:

window.open('http://example.com/someImage.png'); 

我只是堅持有規律的鏈接,如果我是你。

+0

目前在Chrome,我必須刷新看到的圖像。也使用絕對URL – TheRealChx101

+2

爲什麼你會認爲他正在研究面向客戶的代碼,而不是調試工具或爲特定用途構建非常有用的功能?他問了一個抽象問題。他值得一個抽象的答案。 – JayB

-3
window.Open("http://yourdomain.com/yourimage.gif"); 
+1

你可能想編輯你的答案。 'window.open'不是一種方法,如果你嘗試使用它而不是'window.open',會導致錯誤。 –

0

喜歡的東西

window.open(url,'htmlname','width=largeImage.stylewidth,height=largeImage.style.height,resizable=1');} 

但你可能會遇到麻煩運行,如果有人使用的AdBlock或任何彈出窗口阻止程序。

1

嘗試:

<img src="URL or BASE64" onclick="window.open(this.src)"> 
+0

我從Chrome中得到這個錯誤:'''不允許將頂部框架導航到數據URL''' – Mason