0

In Page index.php i have this code to load a page on div. The idea is have a master template and load dynamically content on a div.

<script>
async function loadPage(page){
    var page = page;
    let url = page + '.php';
    content.innerHTML = await(await fetch(url)).text();
}
</script>
<button onclick="loadPage('settings')">Settings</button>
<button onclick="loadPage('users')">Users</button>
<div id="content"></div>

Each page has his own html and javascripts, settings.php has html and some javascript functions:

<script>
function helloWorld(){
    alert('helloWorld');
}
</script>
<button onclick="helloWorld()">Click</button>

The page load fine, but i cant excecute the helloworld() and i get this error on console:

(index):1 Uncaught ReferenceError: helloWorld is not defined at HTMLAnchorElement.onclick ((index):1)

, but if the function is on index.php runs fine. My guess is javascript not detect the new function loaded.

Musikdoktor
  • 144
  • 2
  • 13

1 Answers1

0

There is a thread here that explains the various options and that innerHTML can't be used to inject JavaScript.

1mikevdm
  • 11
  • 3