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'); } }); }
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With