我不能在javascript函数中使用php include

I try to do in a javascript function this:

document.getElementById('menuwrap').innerHTML = '<?php include("deffiles/menudefde.html")?>';

Whenever I insert the php part, my whole javascript stopped working. So nothing of the rest is execute any more. Anyone to help me?

Edit: this:

document.getElementById('menuwrap').innerHTML = '<?php include("deffiles/menudefde.html")?>';

Became online this:

document.getElementById('menuwrap').innerHTML = '<html>
<body>
<div id="menu">
<ul>
<li id="menutext1"><a href="home.html">Home </a></li>
<li id="menutext2"><a href="afbeeldingen.html">Bilder</a></li>
<li id="menutext3"><a href="links.html">Links</a></li>
<li><a href="contact.html">Contact</a></li>

</ul>
</div>';

Edit:

What i want is that i can change the buttons of my menu, which are stated in a html file. So i use javascript for the buttons, but the file is called with php. Is there another way to do so?

You probably have some character in your /menudefde.html file that is illegal in a Javascript string. Most likely a newline character, you could have an unescaped ' as well, which would terminate the string.

You'll need to escape every such character if you want to include it in a string like that.

However, there is probably a better solution to your overall problem.

If the php code is correctly interpreted by your server your .html file might not be a valid string in JS.

Please check if it has no unescaped new lines, no ' characters etc or escape them.

Also check your console log if you see such errors.

You could use the json_encode function to escape that characters.

It's a strange method to use JS and PHP. But, if you want to do this:

if you don't need to execute PHP in your include, you can make it like this:

document.getElementById('menuwrap').innerHTML = '<?php print str_replace("'", "\\'", file_get_contents('deffiles/menudefde.html')); ?>';