For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Thursday, January 19, 2023

pass and get data in createElement statement in javascript

How to pass and get data in createElement statement in javascript?

Here is the listed below code:

In "a1.html"
<script>
var myscript= document.createElement('script');
myscript.setAttribute('data-id1','fordata1');
myscript.setAttribute('data-id2','fordata2');
myscript.setAttribute('data-id3','fordata3');
myscript.setAttribute('data-id4','fordata4');
myscript.setAttribute('data-id5','fordata5');
myscript.setAttribute('src','get-id-in-js.js');
document.head.appendChild(myscript);
</script>

 

 In "get-id-in-js.js" file

var scripts=document.getElementsByTagName("script");
console.log(scripts[0].getAttribute("data-id1"));
console.log(scripts[0].getAttribute("data-id2"));
console.log(scripts[0].getAttribute("data-id3"));
console.log(scripts[0].getAttribute("data-id4"));
console.log(scripts[0].getAttribute("data-id5"));


 

 

 

Share:

0 comments:

Post a Comment

How to make center align child div

 How to make center align child div?   Suppose you have 2 Div tag. And want the make inner tag center <div id="d1">      ...

Ads Inside Post

Powered by Blogger.

Arsip