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) .

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e48ebb-44531.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e48ebb-44531.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?