xRobot
xRobot

Reputation: 26567

Strange Opera issue about window.open

I am designing a new bookmarklet and I am testing it on Opera.

This is my code:

javascript:(function(){var a=window.open('http://www.google.com','Ok','left='+((window.screenLeft||window.screenX)+10)+',top='+((window.screenY||window.screenTop)+0)+',height=200px,width=400px,resizable=1,alwaysRaised=1,location=1,links=0,scrollbars=0,toolbar=0');window.setTimeout(function(){a.focus()},250)})();

The problem is that the new window doesn't appear on the top but at about 200px from the top ( in other browsers like internet explorer, firefox and chrome it works well ).

So, Why this window doesn't appear on the top in Opera ?

Upvotes: 1

Views: 850

Answers (1)

Dmitry Pashkevich
Dmitry Pashkevich

Reputation: 13516

Your problem comes from the fact that in Opera, both window.screenY and window.screenTop are defined, and screenY seems to always equal 0 which is a falsy value in JavaScript, so

window.screenY||window.screenTop

would always return window.screenTop in Opera while you actually need the first value.

The proper way of checking is:

'screenY' in window ? window.screenY : window.screenTop

Here is your complete code, I also updated the check for screenLeft / screenX:

javascript:(function(){var a=window.open('http://www.google.com','Ok','left='+(('screenLeft' in window ? window.screenLeft : window.screenX)+10)+',top='+(('screenY' in window ? window.screenY : window.screenTop)+0)+',height=200px,width=400px,resizable=1,alwaysRaised=1,location=1,links=0,scrollbars=0,toolbar=0');window.setTimeout(function(){a.focus()},250)})();

Good luck.

Upvotes: 3

Related Questions