2013-01-07 20 views
-1

UPDATE:window.open集中在形式提交(關)

答案可以在這裏找到:

center window.open on form submit

=========== ===========================================

我在嘗試將表單提交到以屏幕爲中心的彈出窗口中。我有「frankensteined」幾個js snippets,我發現谷歌搜索時。

請記住,我可以理解HTML/CSS,但不是JavaScript。

<html> 

    <head> 
    <script type="text/javascript"> 
     function directions(sacred) { 
     var x = screen.width/2 - 700/2; 
     var y = screen.height/2 - 450/2; 
     window.open(sacred.action, 'Directions', 'height=485,width=700,left=' + x + ',top=' + y); 
     } 
    </script> 
    </head> 

    <body> 
    <form action="http://maps.google.com/maps" method="get" target="Directions" 
    onsubmit="Directions=directions(sacred)"> 
     <p> 
     <input class="directions-input" id="saddr" name="saddr" type="text" placeholder="enter zip-code" 
     value="enter zip-code" onfocus="this.value = this.value=='enter zip-code'?'':this.value;" 
     onblur="this.value = this.value==''?'enter zip-code':this.value;" /> 
     <input type="submit" class="directions-submit" /> 
     <input type="hidden" name="daddr" value="210+East+Northampton+Street,+Bath,+PA" 
     /> 
     <input type="hidden" name="hl" value="en" /> 
     </p> 
    </form> 
    </body> 

</html> 

fiddle is here

的形式提交到一個新的標籤,並沒有運行功能或window.open

由於任何人提前。

P.S.我只是得到這個stackoverflow的東西。

+0

什麼不正確?你還沒有問過問題。 – Madbreaks

+0

@Madbreaks是的,我忘了那一部分。表單提交給新選項卡,並且不運行函數或window.open。 – jamesJosephFinn

回答

0

你已經寫好了,但在函數中使用它總是更好,所以你不會迷路。

使用此功能:

function openCenteredWindow(url, name) { 
    var width = 700; 
    var height = 450; 
    var left = parseInt((screen.availWidth/2) - (width/2)); 
    var top = parseInt((screen.availHeight/2) - (height/2)); 
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top; 
    myWindow = window.open(url, name, windowFeatures); 
} 

你會看到有什麼區別。

+0

感謝您的幫助。我不知道如何將這個腳本與我的表單標記連接起來。 – jamesJosephFinn

0

要不提交表格您的onsubmit功能必須return false

function directions(sacred) { 
    var x = screen.width/2 - 700/2; 
    var y = screen.height/2 - 450/2; 
    window.open(sacred.action, 'Directions', 'height=485,width=700,left=' + x + ',top=' + y); 
    return false; 
    } 

和形式:

<form action="http://maps.google.com/maps" method="get" target="Directions" 
    onsubmit="return directions(sacred);"> 

我不能完全肯定這個變量sacred的來源。您還將當前的directions()函數的返回值存儲到Directions中,但該函數當前不返回任何內容。但是我無法從你的代碼中弄清楚這一點,但是通過上面的例子,你應該知道如何讓它工作。

+0

感謝您的幫助。我無法讓它工作。這就像一個魅力:'

我的問題是我如何將一個居中的彈出腳本連接到我的html表單標記? – jamesJosephFinn