3

I have one main html file which looks like this:

    <head>
css here
    </head>

    <body>

    <div class="nav">
    ....
    </div>

<div id="mainpage-load">

    <div id="loading">
        <img src="./img/loading-white.gif">
    </div>

</div>

<script> jquery </script>
<script> slider.js </script>
<script> ... </script>
<script> APP.JS </script>

i want to dynamically load content into the div "mainpage-load". but when i load the content with jquery/ajax .load into the div, the javascript file for a slider for example is not effecting the new content.

app.js to load content:

$(function(){
    var $loading = $('#loading').hide();
    $(document)
      .ajaxStart(function () {
        $loading.show();
      })
      .ajaxStop(function () {
        $loading.hide();
      });

    $('#load-it').on('click', function(e){
      e.preventDefault();
      $( "#mainpage-load" ).load( "pages/main.html" );
    })
});

2nd html file (pages/main.html):

<div class="new content">
new content here
</div>

i tried putting app.js and jquery into the but still not working or putting the slider.js in the pages/main.html

thanks in advance !

2
  • 1
    before I write an answer do you call script like I am seeing in your main html page? Script files should be called like this <script src="jquery.js"></script> Commented Dec 18, 2016 at 10:53
  • no just an example ^^ Commented Dec 18, 2016 at 10:59

2 Answers 2

5

You just try this.

jQuery:

$("#y").load("home.html");

javascript:

function load_home() {
     document.getElementById("content").innerHTML='<object type="text/html" data="home.html" ></object>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

you there @Karl Husten
If there are JS functions defined in home.html (or in .js files linked from home.html), are those functions accesible from the parent HTML page?
1

I wrote a simple example and hope it will help you: you can find a demo here and the solution files here

html part :

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <link rel="stylesheet" href="style.css" />
</head>
<body>
  <div class="nav">

  </div>

  <div id="mainpage-load">
    
    <div id="loading" >
       <img src="./img/loading-white.gif">
    </div>

  </div>
  <button id="loadcontent">Load Content</button>


  <script     src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script src="app.js"></script>
</body>
</html>

JavaScript app.js :

(function($){
  $(document).ready(function(){
   
     $(document).on("click", "#loadcontent", function(){
        $(document).load( "pages/main.html", function(resp, status, xhr){
            $("#loading").show();
            if (status == "success" && xhr.status == 200)
              $("#mainpage-load").prepend(resp);
            else{
                console.log("something wrong happend!");
            }
            $("#loading").hide();
        });
       
     });
    
  });
})(window.jQuery);

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.