I am trying to get a dynamically generated link to open after an ajax request (Specifically after success is returned from the AJAX call). I have a partially working code which works on desktop Chrome and Firefox, but it doesn't work on mobile Safari (the window is not opened.)
Here is the code (simplified for clarity purposes):
//this function gets called by a click event
function go(){
var generatedurl
// generatedurl gets generated
$.ajax({
url:'url.php',
type: 'post',
data: {data},
dataType: "json",
success: function(data) {
if (data.status == 'success'){
gofunction();
}
else if (data.status == 'error') {
alert("error");
}
},
error: function(){
alert("error");
}
});
function gofunction() {
window.open(generatedurl, '_blank');
location.reload();
}
}
I am aware of this answer, however when i try to apply it to my code, like this:
//this function gets called by a click event
function go(){
var generatedurl
// generatedurl gets generated
var windowReference = window.open(); //this was added
$.ajax({
url:'url.php',
type: 'post',
data: {data},
dataType: "json",
success: function(data) {
if (data.status == 'success'){
gofunction();
}
else if (data.status == 'error') {
alert("error");
}
},
error: function(){
alert("error");
}
});
function gofunction() {
windowReference.location = url; //this was changed
location.reload();
}
}
For some reason it opens the link even when an error is returned, also location.reload();
doesnt fire.
I'd be thankful for any pointers on what would be the best approach.