6

Here is my index.html file

<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8>
    <title>title</title>
    <meta name=viewport content="width=device-width, initial-scale=1">
    <meta http-equiv=X-UA-Compatible content="IE=edge">
    <link href=assets/css/elegant-icons.min.css rel=stylesheet type=text/css media="all"/>
    <link href=assets/css/bootstrap.css rel=stylesheet type=text/css media="all"/>
    <link href=assets/css/theme.css rel=stylesheet type=text/css media="all"/>
    <link rel=stylesheet type=text/css href="assets/css/style.css"/>
  </head>
  <body>
    <div id="inc"></div>

    <div class=main-container>
      <section class="no-pad coming-soon fullscreen-element">


      </section>
    </div>

    <script src=assets/js/jquery.min.js></script>
    <script src=assets/js/bootstrap.min.js></script>
    <script src=assets/js/smooth-scroll.min.js></script>
    <script src=assets/js/scripts.js></script>
    <script>
      $(function(){
        $("#inc").load("header.html");   
      });
    </script> 
  </body>
</html>

If I copy-paste the content of header.html page after the body, then everything works fine.

when I tried to include the header.html page using .load() function then the CSS won't work properly.

Here is the online sample codepen
if I include the content of div="inc" from an external file like header.html than drop-down menu will overlap each other.

8
  • 1
    And what is inside header.html ? Commented Sep 7, 2016 at 11:08
  • I think its not laod(). It should be load(). Commented Sep 7, 2016 at 11:09
  • Share an example please Commented Sep 7, 2016 at 11:10
  • @Harsh, as per my knowledge load() method is already depricated in latest jquery. See (stackoverflow.com/questions/12643160/load-method-deprecated). Commented Sep 7, 2016 at 11:11
  • thanks for comment, In header file i have some div with contented like drop-down menu after including the layout of drop-down menu is not perfect. with the code in same file everything is working fine Commented Sep 7, 2016 at 11:14

5 Answers 5

1

Hope this helps.

Your scripts.js file contains

$(window).load(function(){

  "use strict";
    // Align Elements Vertically

    alignVertical();
    alignBottom();

    $(window).resize(function(){
        alignVertical();
        alignBottom();
    });

    // Isotope Projects
});

The scripts.js file you have provided is trying to add some styling to the header.html.

but it's not doing the expected behaviour because the scripts.js file is loading before the header.html file.

Just add this at the end after your header content

<script src=assets/js/scripts.js></script>

This will let the header.html content load first and than scripts.js

Also here is the github repo code structure

https://github.com/siddhartharora02/jsLoad

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

Comments

0

Try this

<link href="../assets/css/elegant-icons.min.css" rel="stylesheet" type="text/css" media="all"/>
<link href="../assets/css/bootstrap.css" rel="stylesheet" type="text/css" media="all"/>
<link href="../assets/css/theme.css" rel="stylesheet" type="text/css" media="all"/>
<link rel="stylesheet" type="text/css" href="../assets/css/style.css"/>

<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
<script src="../assets/js/smooth-scroll.min.js"></script>
<script src="../assets/js/scripts.js"></script>

Comments

0

For your original issue,

If you want to include html content dynamically, here is a method to do it,

HTML,

<link data-include="includes/header.html">

JS,

$('link[data-include]').each(function(){
    var el = $(this),
        template = $(this).data('include');

    $.get(template, function(data){
        $(el).replaceWith(data);
    });
});

Hope this will help!


Try this,

Now you can include any partial content as you want.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>title</title>
<meta name=viewport content="width=device-width, initial-scale=1">
<meta http-equiv=X-UA-Compatible content="IE=edge">
<link href=assets/css/elegant-icons.min.css rel=stylesheet type=text/css media="all"/>
<link href=assets/css/bootstrap.css rel=stylesheet type=text/css media="all"/>
<link href=assets/css/theme.css rel=stylesheet type=text/css media="all"/>
<link rel=stylesheet type=text/css href="assets/css/style.css"/>
</head>
<body>

<link data-include="header.html">

<div class=main-container>
<section class="no-pad coming-soon fullscreen-element">


</section>
</div>

<script src=assets/js/jquery.min.js></script>
<script src=assets/js/bootstrap.min.js></script>
<script src=assets/js/smooth-scroll.min.js></script>
<script src=assets/js/scripts.js></script>
<script>
$(function(){
  $('link[data-include]').each(function(){
    var el = $(this),
        template = $(this).data('include');

    $.get(template, function(data){
        $(el).replaceWith(data);
    });
  });   
});
</script> 
</body>
</html>

4 Comments

Thanks for answering, This is including my header like .load() function but unfortunately I am facing the layout issue in this also.
OK can you provide the actual sample? Because without looking at it can't exactly say what is the issue. You can use something like jsbin.com
I have multiple CSS/ JS file where i can upload my sample code. Sites like jsfiddle allows only single files.
You can add multiple files. And use jsbin. Because it shows full html. So its easy for us to check the file importing order.
0

Try using like this,

$(document).ready(function() {
    $.get('header.html').success(function(data){
           var headerHTML = data;
           $('#inc').html(headerHTML);
    });
});

3 Comments

The same code is not showing anything also there is no error in console Please explain what #div_content means ? where I have to declare this div in my code I am using a div call "inc"
The answer has been edited. Please check it once in your page. This is working fine for me.
by this the header will include but the layout problem not solved here is the sample code you can check it codepen.io/anon/pen/LRVQoN
0

How about this:

Instead of using a load method, use an iframe

<iframe id="inc" src="header.html" style="border:0; overflow:auto;"></iframe>

 

Edit

<iframe id="inc" src="header.html" class="hidden"></iframe>

css

iframe.hidden {
  padding: 0;
  margin: 0;
  border: 0;
  overflow: auto;
  display: hidden;
}

iframe.hidden.load {
  padding: 0;
  margin: 0;
  border: 0;
  overflow: auto;
  display: block;
}

JS

!!! Here trigger means the trigger when you want it to load such as onClick, onMouseover, etc !!!

Requires jQuery

$('iframe.hidden').trigger(function() {
  $(this).addClass('load');  
});

4 Comments

Don't use iframe, unless you are load a page outside from your domain. :)
iframe is increasing the issue because it automatically loads <html><body> this type of tags so still facing layout issue
1. You can style the frame using css and that can solve the issue
2. You can add a class to the iframe that does display:hidden; and when you want it to load, use JS to change the style to display: block;

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.