Reputation: 51
This method basically open a new web page in new window where the Url comes in e.result parameter , Here I want to open the same window in New Tab not in new window.
$("#formMain").attr("action", e.result);
$("#formMain").attr("method", "post");
$("#formMain").attr('target', '_blank');
$("#formMain").submit();
any suggestion will be highly appreciated
Upvotes: 0
Views: 6994
Reputation: 299
I have a same request from my client, client want to open action in new tab we succesfully done that work by adding target= "_blank" to form attribute but we encountered another problem then.. after we submit form it open cart first time but when we click on submit button again it not open cart page again after searching for solution i finally made a solution for me..
Now i made jquery which open action url in new tab on submit button form after a delay of 1 second and this function works like a charm..
here is my click page Page Link
My Code:
jQuery(document).ready(function(){
jQuery('.pagefly-container form').on('submit',function(e){
setTimeout(function () { window.open('https://baaboo.shop/cart','_blank');},1000);});})
Upvotes: 0
Reputation: 5392
Generally speaking, it is unwise to submit a form to a page that the user will see because it can cause multiple submissions (if the user goes forward/back, they reopen the tab, etc). Since you are using jQuery already, use $.post() or $.get() instead. (See http://api.jquery.com/jQuery.post/ and http://api.jquery.com/jQuery.get/). The script located at e.result should simply handle the form submission (save stuff to a DB, probably) and return a result. The user would then see a separate page that is based on that result.
So your code would look something like:
$("#formMain").submit(function() {
return false;
});
function submitForm(url)
$.post(url, $("#formMain").serialize(), function(result) {
window.open('resultPageURL&result='+encodeURIComponent(result), '_blank');
});
}
submitForm(e.result);
Obviously the format of the result is up to you - you could just as easily return a json object or the url of the result page if you wanted, using:
try {
var obj = $.parseJSON(result);
window.open('resultPageURL&'+JSON.stringify(obj), '_blank');
} catch (e) {
alert('Not a valid JSON result');
}
or
window.open(result, '_blank');
Upvotes: 1
Reputation: 204
Unfortunately, you cannot set this property from JavaScript. It is the browser which decides whether to open the link in a new window or a new tab according to its settings.
Upvotes: 2