<button type="button">
<a onclick="viewPDF(this,id)" href="" target="_blank"></a>
</button>
function viewPDF(obj,as){
$.ajax({
url:"url",
type:"post",
dataType:"json",
data:{
id:as
},
success:function(data){
if(data.data!==null){
$(obj).attr("href",data.data)
window.location.href=data.data;
}
},
error:function(){
console.log("")
}
})
html