5

The issue I am having is when I try to do something like the below code, the window will be blocked by pop-up blockers. I am using getScript so that I can make cross domain requests. I am using jQuery 1.4.2 to do the below.

Example of code that will be blocked:

//Code that gets blocked by pop-up blockers
$(document).ready(function(){
    $(".popup").click(function(){
        $.getScript("URL_To_A_Javascript_File", function(){
            window.open("dynamicURL", "_blank");
        });
    });
});

Example of code that gets past blockers, but doesnt get URL in time:

//This code will get past the pop-up blocker, but the var url won't be updated 
//with the dynamicURL before the window.open() fires in browsers 
//like safari or chrome.
$(document).ready(function(){
    var url;
    $(".popup").click(function(){
        $.getScript("URL_To_A_Javascript_File", function(){
            url = "dynamicURL";
        });
        window.open(url, "_blank");
    });
});

How can I open a new window using a URL that is generated inside the getScript callback function, and avoid pop-up blockers?

Adam Link
  • 2,783
  • 4
  • 30
  • 44
Barry Taylor
  • 113
  • 1
  • 7
  • fi youre just using this for XD, why do you need to use `window.open`? – prodigitalson Nov 04 '10 at 13:51
  • The reason I need to use XD ajax is because the javascript file I am calling is hosted in a Marketing CRM, and is returning a script that contains information about the user which I need to determine what page to pop-up. – Barry Taylor Nov 04 '10 at 14:27

3 Answers3

6

Ok, it looks like I finally figured out how to do what I was trying to do.

This way allows me to do the pop-up with out the need for an intermediate page that handles the javascript.

var newWin;
$(document).ready(function(){
    $(".popup").click(function(){
        newWin = window.open();

        $.getScript("URL_To_A_Javascript_File", function() {
            newWin.location = "DynamicURL";
        });
        return false;
    });
});
Barry Taylor
  • 113
  • 1
  • 7
1

You can't avoid popup blockers, and let us all give thanks for that.

When your code opens a window from some event loop that's not the direct result of user action (mostly that means a "click" event), the browser assumes that the user should have a choice of whether to see the new window.

In the case of something like your "getScript", the handler that's called when the script has been gotten is in one of those kinds of non-user event loops, so the blocker rules apply.

You could, perhaps, run your "getScript" code from your new window. The browser will allow the window to be opened from that "click" handler.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • ATM I am opening a new window that then redirects to the page it needs after doing the getScript code on document.ready(), but I would like to avoid doing it this way. I basically need a way to run the getScript after a user clicks a link, and then when the getScript is finished and has updated the var URL, open the window. – Barry Taylor Nov 04 '10 at 14:18
  • I understand the frustration, but there's just no mechanism that lets you give the browser some reassurance that you're not evil. – Pointy Nov 04 '10 at 14:20
0

Simply don't work with popup, use something like Lightbox instead.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208