views:

215

answers:

2

Goal: quick and dirty app (client side only) to grab some arguments from one page and put results onto a new page, which can be printed and then closed. Arguments on the original page then can be changed and new page popped.

Used this as a starting point: https://developer.mozilla.org/en/DOM/window.open http://www.yourhtmlsource.com/javascript/popupwindows.html

Proof of concept(final version will have about 10 inputs/args)

HTML fragment

<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);
  }
};

This works(tested on FF3.5 and Chrome), except new window does not pop into center. How to center it? Mozzila says needs chrome=yes and talks about UniversalPrivilege scripts, what kind of beasts are those?

Anything else that can be improved?

+1  A: 

You'll need to set the top and left properties instead of center=1.

var left = (screen.width - windowWidth) / 2;
var top = (screen.height - windowHeight) / 2;
Gabriel McAdams
+1  A: 

Here's one of my custom cross-browser scripts that can be reused dynamically to center any popped window of any size on the screen:


// 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

This will do the trick considering it truly is a cross-browser implementation, including reverse compatibility to browsers in place back in 2001. It also contains a check to make sure the end-user has popup windows enabled.

drlouie - louierd