2009-12-30 42 views
0

目標:快速和骯髒的應用程序(僅客戶端)從一個頁面抓取一些參數並將結果放到一個新頁面上,該頁面可以打印然後關閉。然後可以更改原始頁面上的參數並彈出新頁面。以Javascript爲中心彈出窗口並傳遞html的正確方法

以此爲起點: https://developer.mozilla.org/en/DOM/window.open http://www.yourhtmlsource.com/javascript/popupwindows.html

概念驗證(最終版本將具有約10的輸入/參數)

HTML片段

<input type="text" id="x"> 
<form> 
<input type=button value="Calculate" onClick="javascript:genResults()"> 
</form> 

JS

function dirtypop(arg) 
{ 
    var popwin=window.open('','name','height=300,width=400,status=1,center=1'); 

    popwin.document.write('<html><head><title>Square</title>'); 
    popwin.document.write('</head><body>'); 
    popwin.document.write('<h1>Squared plus one is: '+arg+'</h1>'); 
    popwin.document.write('<p><a href="javascript:self.close()">Close</a> this window</p>'); 
    popwin.document.write('</body></html>'); 
    popwin.document.close(); 
}; 
function genResults() 
{ 
    x = document.getElementById('x').value; 
    if (x == parseFloat(x)) 
    { 
    dirtypop(x*x+1); 
    } 
}; 

這可以工作(在FF3.5和Chrome上測試),除非新窗口沒有彈出到中心。如何居中? Mozzila說需要chrome = yes並且談論UniversalPrivilege腳本,那些是什麼樣的野獸?

還有什麼可以改進的?

+0

感謝您的回答,我的新年瑣事計劃取得了成功! :) – Sint 2010-01-05 07:40:50

+0

由於實際上有幫助,請介意給我提高回答的答案?謝謝! – DoctorLouie 2010-01-05 21:22:17

回答

3

這裏是我的自定義跨瀏覽器的腳本之一,可以動態地被重用到中心任何規模的彈出窗口在屏幕上:


// here's the script 
function popWindow(url,winName,w,h) { 
    if (window.open) { 
     if (poppedWindow) { poppedWindow = ''; } 
     //GET SIZE OF WINDOW/SCREEN 
     windowW = w; 
     windowH = h; 
     var windowX = (screen.width/2)-(windowW/2); 
     var windowY = (screen.height/2)-(windowH/2); 
     var myExtra = "status=no,menubar=no,resizable=yes,toolbar=no,scrollbars=yes,addressbar=no"; 
     var poppedWindow = window.open(url,winName,'width='+w+',height='+h+',top='+windowY+',left=' + windowX + ',' + myExtra + ''); 
     setTimeout(refreshThis,3000); 
    } 
    else { 
     alert('Your security settings are not allowing our popup windows to function. Please make sure your security software allows popup windows to be opened by this web application.'); 
    } 
} 
 

// and you would call it like this: 
popWindow('http://www.myurl.com/','myPoppedWindowName','500','400'); 

// With this example call you would pop a window with a url of http://www.myurl.com/ 
// which is given the name of myPoppedWindowName 
// and a width of 500px along with height of 400px 
// which gets centered on the screen according to these size parameters 
 

這將這樣的伎倆考慮它確實是一個跨瀏覽器的實現,包括在2001年對瀏覽器的反向兼容性。它還包含一個檢查以確保最終用戶已啓用彈出窗口。

+0

優秀的答案,更多的網站應該確保用戶知道在大多數現代瀏覽器中默認情況下彈出窗口是被禁用的。 – 2012-12-15 20:43:26

1

您需要設置頂部和左側屬性,而不是center = 1。

var left = (screen.width - windowWidth)/2; 
var top = (screen.height - windowHeight)/2;