calling the next javascript via eval, remove the span tag created by the initial JS file

45 views Asked by At

I have a html file and 2 JavaScript files: mainscript.js and script1.js. I inject the script1.js inside the mainscript.js. However, what happens is that by calling script1.js, the htmltags created by mainscript.js got removed. Any idea why this happens?

html code:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Linear Call</title>
    
</head>

<body>
    <div id="main"><p>hi</p></div>
    <script src="js/recursion_linear/MainScript.js">
    </script>
</body>

</html>

mainscript.js:

const loadScript = async(url) => {
    const response = await fetch(url)
    const script = await response.text()
    eval(script)
}
var s = document.createElement("span");
document.write("<br>");
s.innerText="This is main script";
s.id="mainscript";
document.body.append(s);


const scriptUrl_1 = "js/recursion_linear/Script1.js"
loadScript(scriptUrl_1)

script1.js:

document.write("<br>");
var s = document.createElement("span");
s.innerText="This is Script1";
s.id="script1";
document.body.append(s);

The output is

This is Script1

While the expected one is

This is main script This is Script1

0

There are 0 answers