0

I am loading a header with navigation menu using

jQuery(document).ready(function() {
    $('#header').load("header.html");
});

and after this code I have the following

$(window).load(function() {
   $('#about').addClass('active');
});

from everything I understand, the window.load runs after document.ready, but it's still not applying anything. Any help? The jquery for addClass seems correct as when typed into a console after loading up the page in a browser, it works.

Is this a problem with the way I have set up my header template and loading it?

Previously I worked with php and some node.js, so re-using code was straightforward but I have a requirement to use pure html/js, hence the header template.

thanks

3
  • 'about' id is inside header.html file? Commented Sep 19, 2016 at 10:07
  • yes this is correct. apologies I should of made that clear Commented Sep 19, 2016 at 10:08
  • .load event does not fire on $().load - they might have the same name, but they're very different actions. Commented Sep 19, 2016 at 10:08

2 Answers 2

6

Try doing it in the load call back instead:

$('#header').load("header.html", function () {
   // this is run after header.html has been loaded
   $('#about').addClass('active');
});

More information about .load

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

Comments

3

You want to pass a callback to load which should run after the content has loaded

jQuery(document).ready(function() {
    $('#header').load("header.html", function(){
       $('#about').addClass('active');
    });
});

To understand the differences between document ready and window load check this: window.onload vs $(document).ready()

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.