2013-04-24 125 views
11

我正試圖將href加載到同樣位於屏幕中的特定維度的彈出窗口中。使用Javascript在彈出窗口中打開鏈接

這裏是我的源:

<li> 
    <a class="sprite_stumbleupon" href="http://www.stumbleupon.com/submit?url=http://www.your_web_page_url" target="_blank" onclick="return windowpop(545, 433)"></a> 
</li> 

這裏是我的javascript:

function windowpop(url, width, height) { 
    var leftPosition, topPosition; 
    //Allow for borders. 
    leftPosition = (window.screen.width/2) - ((width/2) + 10); 
    //Allow for title and status bars. 
    topPosition = (window.screen.height/2) - ((height/2) + 50); 
    //Open the window. 
    window.open(url, "Window2", "status=no,height=" + height + ",width=" + width + ",resizable=yes,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no"); 
} 

這似乎測試時返回404錯誤。我究竟做錯了什麼?

感謝堆。

+2

您還沒有發送的'url'參數到'windowpop'功能。 – 2013-04-24 03:44:10

+0

函數windowpop(url,width,height)'函數期待你返回一個URL,但你只返回寬度和高度。 – Mal 2013-04-24 03:44:25

+0

這可能會因爲一個很好的原因而關閉,但我發現它非常有幫助。我不知道這些額外的參數window.open。 – jstaab 2014-06-27 20:22:32

回答

12

function windowpop(url, width, height)該函數需要返回一個URL。

onclick="return windowpop(545, 433)"您只返回widthheight

,請返回使用this.href的網址:

onclick="return windowpop(this.href, 545, 433)" 

例如:http://jsfiddle.net/zKKAM/1/

+2

只是一個說明,我使用這種方法,並且如果您不允許彈出窗口在您的瀏覽器上正常工作,但是,如果您這樣做,它會打開彈出窗口兩次。我通過添加event.preventDefault()來解決它。點擊鏈接,並從windowpop()的開始處刪除返回。 – madagalbiati 2013-10-18 18:13:37