Home > JavaScript > Popup-under hack

Popup-under hack

tn500w_javascript-logo

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 on the window which is currently opened. So, in this case, your window loading targetURL will get focus. And, suppose your boss says to focus on the parent window and not the window with the targetURL. There are a lot of posts available over the internet that advises using the 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 the background as malicious. So let me provide you away 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 rendered in a loop. To stop this just bind a suitable logic to it, maybe bind it to a session so that it gets loaded once in a session.

About jkoder