简体   繁体   中英

Express response not adding html attribute value in anchor tag href

I'm sending express response using Nodejs script. In this, I m sending HTML text as a response and also setting href values of the anchor tag, but I m not able to see it on the client side. while I m able to see it on the innerHTML of the tag.

I m focusing on the issue as you can see in the below line of code. (Below are the two lines in my code in which I m facing the issue of adding/appending href values.)

res.write("<p>("+(counter+1)+") "+"<a href='https://patents.google.com/patent/'"+toString(patent_number)+" target='_blank' > "+patent_number+"</a>"+", score: "+patent_A[i]['score']+"</p>")
res.write("<p>click <a href='"+"data:text/csv;charset=utf-8,'"+encodeURI(csv)+"' target='_blank' download='output.csv' >here</a> to download csv file</p>")

Also, I tried to add an event listener by sending a script of the download button, it also not worked.

res.write("<script> document.getElementById('link').innerHTML = 'Hello World' document.body.onload = function(){  function download_csv() { var hiddenElement = document.createElement('a') ")
res.write("  hiddenElement.href = 'data:text/csv;charset=utf-8,' "+ encodeURI(csv))
res.write("  hiddenElement.target = '_blank' ")
res.write("  hiddenElement.download = 'output.csv' } }")
res.write(" </script>")

Please see code below complete code of Nodejs and screenshot.

res.writeHead(200, {'Content-Type': 'text/html'});
res.write("<body>")

res.write("<center><div style='border-style: solid;width: 600px;margin-top: 10%;text-align: left; padding:10px;'>")

// <p>Following patents constitute the top 20% of the portfolio</p>
if(patent_A.length != 0){
    res.write("<p><b>Following patents constitute the top 20% of the portfolio</b></p>") 
    for(var i=0; i<patent_A.length;i++){
        counter = i
        patent_number = patent_A[i]['patentNumber']
        res.write("<p>("+(counter+1)+") "+"<a href='https://patents.google.com/patent/'"+toString(patent_number)+" target='_blank' > "+patent_number+"</a>"+", score: "+patent_A[i]['score']+"</p>")
    }
}

// <p>Following patents constitute the top 21-50% of the portfolio</p>
if(patent_B.length != 0){
    res.write("<p><b>Following patents constitute the top 21-50% of the portfolio</b></p>") 
    for(var i=0; i<patent_B.length;i++){
        counter = i
        patent_number = patent_B[i]['patentNumber']
        res.write("<p>("+(counter+1)+") "+"<a href='https://patents.google.com/patent/'"+toString(patent_number)+" target='_blank' > "+patent_number+"</a>"+", score: "+patent_B[i]['score']+"</p>")
    }
}

// Following patents constitute the remaining 51-100% of the portfolio
if(patent_C.length != 0){
    res.write("<p><b>Following patents constitute the top 21-50% of the portfolio</b></p>") 
    for(var i=0; i<patent_C.length;i++){
        counter = i
        patent_number = patent_C[i]['patentNumber']
        res.write("<p>("+(counter+1)+") "+"<a href='https://patents.google.com/patent/'"+toString(patent_number)+" target='_blank' > "+patent_number+"</a>"+", score: "+patent_C[i]['score']+"</p>")
    }
}   

res.write("<p>click <a href='"+"data:text/csv;charset=utf-8,'"+encodeURI(csv)+"' target='_blank' download='output.csv' >here</a> to download csv file</p>")

res.write("<button onclick=download_csv()>Download CSV</button>")

res.write("</div></center>")
res.write("<script> document.getElementById('link').innerHTML = 'Hello World' document.body.onload = function(){  function download_csv() { var hiddenElement = document.createElement('a') ")
res.write("  hiddenElement.href = 'data:text/csv;charset=utf-8,' "+ encodeURI(csv))
res.write("  hiddenElement.target = '_blank' ")
res.write("  hiddenElement.download = 'output.csv' } }")
res.write(" </script>")
res.end()

In both statements you wrote, you misplaced small quotes.

Try below:

res.write("<p>("+(counter+1)+") "+"<a href='https://patents.google.com/patent/"+toString(patent_number)+"' target='_blank' > "+patent_number+"</a>"+", score: "+patent_A[i]['score']+"</p>")
res.write("<p>click <a href='"+"data:text/csv;charset=utf-8,"+encodeURI(csv)+"' target='_blank' download='output.csv' >here</a> to download csv file</p>")

Furthermore, to get rid of difficulty of dealing with complete string literal with values, I recommend using string interpolation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM