It is impossible to dynamically change the address of href according to the data requested by the interface and open it in a new window.

<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
Apr.09,2021

you have confused the function:

window.location.href = 'https://www.baidu.com/'  -- 
window.open("https://www.baidu.com/","_blank")   -- _blank

aonclick="viewPDF(this,id)"button

aa:
<a href="javascript:void(0)" onclick="viewPDF(this,id)">click</a>

window.open (url)


Don't be so troublesome. Just click with button, to get the url asynchronously and then window.open (link) .

Menu