I know that Safari try to block the open of a new tab via window.open() during an ajax call. We need to call window.open() to open a new tab before the ajax call so that the program can prevent the blocking from Safari. Reference for above, window.open(url, '_blank'); not working on iMac/Safari.
However, what if I want to use information from an ajax call to validate whether I need to open a new tab, i.e. conditionally open a new tab depends on the validation of an ajax call?
Then I can't open a new tab before the ajax call. It is because if the validation is not passed, then I need to close the tab that I opened before the ajax call..., which is not the behavior that I want for my program.
How can I make it such that I can open a new tab given the that information are validated from an ajax call? Thanks all for answering!
let newTab = window.open();
someAjaxChecking(...).then((isValid)=>{
// isValid is a return boolean from the ajax calling someAjaxChecking() to
// determine whether redirect the new tab or close it
if(isValid){
newTab.location = url;
}else{
newTab.close(); // ***it is not desire to open and close the new tab...
}
})
The real case:
- I need to send a form(set of data) to my backbend to validate the form(an AJAX calling);
- then it will return a boolean(isValid) to tell whether the form is valid;
- then it will open a new tab if the boolean(isValid) is true