Popup-under hack

Popup-under hack

If you are working on a mobile site, you have a constraint of screen size. What if you are loading a URL from a page using window.open(targetURL,’_blank’);. For your information browser will only focus to the window which is currently opened. So, in this case your window loading targetURL will get focus. And, suppose your boss says to focus to the parent window and not the window with the targetURL. There are a lot of posts available over internet that advises to use below code:-

var myWindow=window.open(targetURL,'_blank');
myWindow.blur();
window.focus();

But wait….due to security reasons modern browsers don’t entertain this feature as they treat the newly opened window in background as malicious. So let me provide you a way for it. All you have to do is to open the currentURL in the new window and in the parent window just refresh the page with targetURL.

var currentURL=window.location.href;
var targetURL='<targetURL>';

window.open(currentURL,'_blank');
document.location.href=targetURL;

And this is not it. With this logic your pages will be rendering in a loop. To stop this just bind a suitable logic to it, may be bind it to a session so that it gets loaded once in a session.