I have a long index.html file I want to separate into sections but all the parts in those should function at once when I’m at index.html. Meanwhile I’m trying to add code to have a new window in my index.html. Since the index file is already long, I need the new window code in a separate html.
I tried jquery .load("") method as said in text text but doesn’t seem to work.
<!-- index.html file -->
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<script>
$(function() {
$("#includeHtml").load("newwindow.html");
});
</script>
</head>
<body>
<h2>This is INDEX PAGE</h2>
<div id="includeHtml"></div>
</body>
</html>
<!-- newwindow.html file -->
<html>
<body>
<h3>This is the NEW WINDOW I want to add to the INDEX</h3>
</body>
</html>
Can someone help me to fine tune this method or suggest another method?
3
Answers
As you seem to use php I would suggest to do it server side:
Be aware that you have to rename the index.html to index.php and the newwindow.html to newwindow.php
If you want to use javascript to load the html you can do it in this way:
Also on top of what Onki Hara said above, you can also use php to include html files as well, not just php files.
If you want to separate your long index.html file into sections while ensuring that all parts function together when you’re on the index.html page, you can consider using HTML’s
<iframe>
tag to embed the separate sections as individual HTML files.Here’s how you can achieve this:
Create separate HTML files for each section you want to separate. For example, if you have three sections, create
section1.html
,section2.html
, andsection3.html
.In your index.html file, create
<iframe>
tags for each section and set the src attribute to the respective section HTML files. For example:This will load and display each section in its own
<iframe>
on the index.html page.To add a new window functionality, you can create a separate HTML file, let’s say
newwindow.html
, with the desired content for the new window.In your index.html file, you can use JavaScript/jQuery to open the
newwindow.html
file in a new window. Here’s an example using jQuery:In this example, a button with the id openNewWindow is created. When the button is clicked, it triggers the JavaScript/jQuery code to open
newwindow.html
in a new window with a width and height of 500 pixels.Make sure that all the necessary HTML, CSS, and JavaScript files are correctly linked and placed in the appropriate locations. Additionally, ensure that you have a compatible version of jQuery included for the code to work properly.
By separating your sections into individual HTML files and using tags, you can keep your index.html file more organized and load each section independently.
…or translate into PHP…