0

I was trying this find this out but too no avail. I was just wondering if you could call another separate html file in your main html.

I know you can do it for javascript <script src = "Classroom_names.js"></script>

So I was wondering if you could do it also for it in html. For example

<script src = "field_names.html"></script>

Or if you could not do it like that would you be able to call a html file in javascript andthen set a condition to it.

Thank you for taking your time to read this guys

  • Does this answer your question? https://stackoverflow.com/a/59249682/8029211 – M A Salman May 16 '20 at 19:29
  • Does this answer your question? [Include another HTML file in a HTML file](https://stackoverflow.com/questions/8988855/include-another-html-file-in-a-html-file) – M A Salman May 16 '20 at 19:30

1 Answers1

0

The easiest way to do it is using jQuery, which you must include in your project, and then use the load function to place it in some node of your website, in this case I used a div with the class "menuContainer" and the menu I have put in a file called menu.html

Important: You must have a web server running on localhost to be able to use this function since if you open the file using the url "file: /// your_file.html" through the browser it will give you a cross-origin error, if you don't have a server web running you can mount it very easily with python3 using the http.server module or in nodejs using http-server.

<!DOCTYPE html>
<html lang="en">
<head>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div class="menuContainer"></div>
</body>

  <script>
    $(document).ready(function () {
      $('.menuContainer').load('./menu.html');
    });
  </script>
</html>
Josué Ayala
  • 285
  • 1
  • 12