1

I'm trying to import an HTML file using jQuery:

$('#section1').load('section1.html');

The problem is that when the code is loaded jQuery doesn't work well. Inside this HTML I have some jQuery UI and some checkbox that if I don't use "load" function work, but if I import it, they are not recognized.

5
  • Where we can see the problem? jsfiddle? Commented Mar 30, 2016 at 14:52
  • 1
    @Daniele Martini, what you will need to do is have a document.ready block for each html file, and be careful that the code/events is just for your html. I dont know if i explained myself Commented Mar 30, 2016 at 14:56
  • Can make me an example of document.ready block? If I try to import section 1 where I have to insert this script (below / on top) and which is the syntax? Commented Mar 30, 2016 at 15:03
  • You only need a script tab into each html file $(document).ready(function() {...}); and will be executed. Which problem do you have? Commented Mar 30, 2016 at 15:24
  • Sorry could you be a little bit more precise? it is the first time that I'm doing it and I'm also not very familiar with Jquery. I created a new file and copied on it my HTML code to import in in the Main page. At the bottom of the main page I used this script $('#section1').load('section1.html'); and in the middle of my page i created a div with section1 ID. Where I have to use your script $(document).ready(function(){...});? could you make me a quik example to better understand? Commented Mar 30, 2016 at 15:42

2 Answers 2

2

Make sure the file path is correct. If using a relative path, make sure you have the file in the correct location.

My working example:

test.html:

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    </head>
    <body>
        <h1>
            Test Header
        </h1>
        <div id="place_holder"></div>
    </body>
    <script>
        $(document).ready(function() {
          $('#place_holder').load("load_me.html");
        });
    </script>
</html>

load_me.html: Placed in the same location as test.html

<h2>Loaded from another file</h2>

You don't have to do in-line Javascript. You can load an external JavaScript file that does the same thing.

Sign up to request clarification or add additional context in comments.

4 Comments

Maybe I didn't tell well my problem. I'm actually importing the external file in my HTML but the external code is not working with jquery. I'm importing a form (labels, inputs, sections, buttons ecc ecc) and I'm using jquery UI and a scroll To library which are not working when I imported the code,
Make sure you wrap your JS file with a '$(document).ready(function() { });' Also, make sure you are loading the jquery library. You can check for JavaScript errors by pressing F12 in Firefox and looking at the Concole Tab. Also, it should tell you if your web page is loading in the jQuery library or getting a 404 not found error by looking ta the Network Tab and reloading the page.
I already tried everithing. The imported code is not recognized from the library imported in the main page
Can you provide more code snippets of how you are loading the JS file and maybe the contents of the JS file and the relevant HTML?
-1

Finally I figured out how to solve this problem. I tried to include the other HTML file using PHP instead of Jquery load function.

I renamed my file with PHP extension and uploaded it on virtual server like MAMP or XAMP).

Then used this sintax to import my code.

<?php  include("your_file_path/your_file_name.html");  ?>

I included a big section of a website inside the mainpage and my other HTML file was in the same folder of the HOMEPAGE. I used

<?php  include("section6.html");  ?>

After use this method all my Jquery library worked correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.