Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

turn a variable into filename for HTML

var filename2 = "PDFxx.pdf";
<object data=filename2 width="800" height="500"></object>

in JavaScript, I like to assign a var filename2, equals to "PDFxx.pdf" etc.

data=filename2 never works, but data="PDFxx.pdf" works fine.

Wondering how to accomplish that. Please advise.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance.

>Solution :

Create the <object> via JavaScript and inject it into your document

const filename2 =
  "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"; // "PDFxx.pdf";
const obj = Object.assign(document.createElement("OBJECT"), {
  data: filename2,
  width: 800,
  height: 500,
});

document.body.append(obj);
object { border: 1px dotted; }

Alternately, query the element and set its data property

const filename2 =
  "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"; // "PDFxx.pdf";
  
document.querySelector("object").data = filename2;
object { border: 1px dotted; }
<object width="800" height="500"></object>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading