0

i am using fetch to load a page code (html & js ) from another page then insert that code to be able to use it.

   fetch('site.com')
    .then(response=>response.text())
    .then(data=>{ document.body.innerHTML += data })

after that i try to call a function called dosubmit() that's in the other page code but it gives me

dosubmit is not defined

even though that the request loaded successfully.

moooon
  • 1
  • too lazy to find the dupe, but javascript is not evaluated with innerHTML – epascarello Dec 14 '20 at 17:14
  • @epascarello "but javascript is not evaluated with innerHTML" but what does that mean ? that js code ( the submit function is in the page directly)... so the fix is ? – moooon Dec 14 '20 at 17:15
  • When you do `foo.innerHTML = ""` it does not evaluate the script tag. – epascarello Dec 14 '20 at 17:26
  • @epascarello "it does not evaluate the script tag." what does evaluate mean ? yeah document.body.innerHTML = "" didn't pop up anything ... so how do i get it to work ? – moooon Dec 14 '20 at 17:31

1 Answers1

0

Javascript execution happens when html page gets loaded. After page has loaded, and you are trying to inject the Javascript code won't help.

There are some ways though to achieve this,

  1. You can make use of eval()
  2. You can dynamically insert a separate js file having your code (In your case dosubmit())