I have a function called save:
function save(callback){
var ed=tinyMCE.get('body');
var data=ed.getContent();
$('#saving').show();
$('#save_err').hide();
$.post('/article/ajax/save',{title:$('#title').val(),body:data,token:token,aid:<?php echo $article_id; ?>},function(d){
$('#saving').hide();
if(d.suc==1){
$('#saved').show();
setTimeout(function(){$('#saved').hide()},2000);
}else{
$('#save_err').html(d.err).show();
}
return true;
},"JSON");
};
When the element #preview is clicked, I would like save to execute, and a redirect to happen after the function completes.
$('body').on('click','#preview',function(){
save(function(){
window.open('/article/preview/<?php echo $article_id; ?>','_blank');
});
});
I have used both the accepted answer and the highest rated answer from here, neither are working. The redirect occurs before the function completes. Execute jquery function after another function completes
What I have also tried:
$('body').on('click','#preview',function(){
$.when(save()).done(function(){
window.open('/article/preview/<?php echo $article_id; ?>','_blank');
});
});
What am I doing wrong?