1

I what to load the b.js file to HTML with button and function. when user type "yes" and click on "Ok" button, "b.js" must load in page (not open in new window or new tab)

what code I must to use in ??????????? place to solve my problem? here is the code:

HOME.html

<html>
<head>
</head>
<body>
<form name="load">yes OR no<input name="id" type="text">
<input type="button" value="OK" onClick="my(document.forms.load)">
<script>
function my(form) {
    if (form.id.value=="yes") {              

?????????????
   
        } else {
            alert("NO")
        }
}
</script>
</body>
</html>

b.js

document.write(`

<html>
<h1> Load the HTML code</h1>
</html>

`);

I tried to use <script src="b.js"></script> but this code immediately load "b.js" that I dont what this

I tried to use

var scriptTag = document.createElement('script');
scriptTag.setAttribute('src','b.js');
document.head.appendChild(scriptTag)

but not working

I tried to use

document.getElementsByTagName('body')[0].innerHTML += "<script src='b.js'></script>";

but this code not working

what can I do to load the "b.js" file after click on button?

thanks :)

2
  • Why would you want to do this? b.js is tiny. Just load it in with the original page. It sounds like you're making things very complicated. Commented Feb 25, 2022 at 20:37
  • @Andy this is for test in here. the original is more than 1260 line code! Commented Feb 25, 2022 at 21:15

1 Answer 1

2

Is this what you're looking gor?

function my(form) {
  const loadHTML = () => {
    const scriptB = document.createElement('script')
    scriptB.setAttribute('src','b.js')
    console.log(scriptB)
    document.write(`
      <h1>Load the HTML code</h1>
      ${scriptB.outerHTML}
    `);
  }
  if (form.id.value==="yes") {
    loadHTML()
  } else {
    alert("NO")
  }
}
<html>
<head>
</head>
<body>
<form name="load">
  <h2>YES or NO</h2>
  <input name="id" type="text">
  <input type="button" value="OK" onClick="my(document.forms.load)">
</form>
</body>
</html>

Sign up to request clarification or add additional context in comments.

1 Comment

So you just want to append the script "b.js" after button click?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.