Reputation: 2694
Only on chrome...
<form action="http://example.com" method="get" target="_blank">
<input type="text" name="somename" value="some val">
<input type="submit">
</form>
exec this js from console, timeout func or whatever...
document.forms[0].submit();
If submit is triggered by actual clicking on "submit" button, form will work normally, but if submit is triggered via javascript it will open popup.
I need form exactly like this (method get, target blank) and submit has to be triggered via javascript because there are several ajax requests to be completed before submit...
Does anyone knows workaround?
FOR: Duncan Cowan
$('button').on('click', function(){
// Validation
// foo
// bar
// ...
// var errors = true;
window.promises = new Array;
// do ajax calls and add hidden inputs...
window.promises.push($.ajax({ /* Call ...*/ }));
window.promises.push($.ajax({ /* Call ...*/ }));
window.promises.push($.ajax({ /* Call ...*/ }));
// not always the case
var promis = true;
// once inputs are placed... submit
if (promis) {
$.when.apply($, window.promises).done(function() {
// Do submit
$('form').trigger('submit');
});
}
// errors or promises cancel submit
if ( errors || promis ) {
return false;
}
});
Upvotes: 0
Views: 902
Reputation: 495
In order for the browser to not class this as a popup you need to trigger the form submit using a trusted event (ie. the user clicks on something, etc). If you need to run ajax before submitting but after the trusted event, you should run it in syncronous mode (async: false
).
For example, the user clicks somewhere, the synchronous ajax request is made, and then the form is submitted.
If you trigger a new window to open using some sort of non user-initiated event the browser thinks it's a popup and will often block it.
OK, instead of running the AJAX through promises, simply run it directly in the click function. However, set 'async' to 'false' so that the code stops and waits for the ajax to complete.
$('button').on('click', function(){
// Validation
// foo
// bar
// ...
// var errors = true;
// do ajax calls and add hidden inputs...
$.ajax({asyc: false, /* Call ...*/ });
$.ajax({asyc: false, /* Call ...*/ });
$.ajax({asyc: false, /* Call ...*/ });
$('form').trigger('submit');
});
Upvotes: 1