Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.open popup getting blocked during click event

What I ultimately need to do is run an $.ajax() call and then after that is run, open a new window.

A use clicks on a "Preview" button that saves their current form then opens a new window that shows a preview of the item with the data that was just saved.

But as-is, the window.open function gets blocked by popup blockers.

Here's the basic parts of my code:

HTML:

<a href="/surveys/185/preview" class="preview" target="_blank">Preview</a> 

JavaScript:

$('.preview').live('click', function(event){   save_survey($(this).attr('href'));   event.preventDefault(); });  function save_survey(url) {   $.ajax({     type: "POST",     url: form_url,     dataType: 'json',     data: form_data,     success: function(data) {       window.open(url, '_blank');     }   }); } 
like image 255
Shpigford Avatar asked Jul 08 '11 18:07

Shpigford


People also ask

What triggers pop up blocker?

Most popup blockers are triggered when a popup is launched indirectly from a user action. Some popup blockers are triggered when a user clicks, but most are not. Basically, if the popup is triggered within a click handler (or code that it calls), you are generally okay.

How do I know if my pop up window is blocked?

Click the ellipsis icon (...) in the upper right corner of your web browser, and then click Settings. In the Advanced settings section, click View advanced settings. In the Block pop-ups section, click the switch to Off. Pop-ups are now allowed.


1 Answers

I ran into this problem recently and found this work-around:

1) call window.open just before calling $.ajax and save window reference:

var newWindow = window.open(...); 

2) on callback set location property of the saved window reference:

newWindow.location = url; 

Maybe it will help you too.

like image 90
Igor Dymov Avatar answered Oct 14 '22 15:10

Igor Dymov