javascript - Add a second function after .load() and first function completed -


i have 2 separate html files being dynamically added index.html means of jquery .load(). after html content has loaded fade-in animation happens. want call yet function -- orientationload() -- depending on html loaded. understand orientationload() being called before load() has completed loading. console error:

typeerror: 'undefined' not object (evaluating '$('.page').classlist.contains')

can help? thanks

function orientationload() {   var viewportwidth = window.innerwidth;   if (viewportwidth > 768) {       landscapeclass();   } else {       portraitclass();   } };  function changepage(filename){     $('.content_wrapper').animate({opacity:0}, 500, function(){         if (filename == 'home.html?v=1'){             $('.page').addclass('home');         }else{             $('.page').removeclass('home');         }         if (filename == 'story.html?v=1'){             $('.page').addclass('story');         }else{             $('.page').removeclass('story');         }     });   // load html jquery's built-in ajax instruction   $('.content_loading_container').load('content/'+filename, function(){     $('.content_wrapper').delay(250).animate({opacity:1}, 500);   });   // if page 'story' call orientationload()   if ($('.page').classlist.contains('story'))     {       orientationload();     } else{} };  $(document).ready(function(){   $('nav a').on('touchstart', function(){     changepage($(this).attr('data-file'));   }); }); 

jquery objects not have classlist property. use classlist, must extract element jquery object.

$('.page').get(0).classlist.contains('story') 

however, can simplify using jquery's .is method or .hasclass method.

$('.page').is('.story'); // true/false $('.page').hasclass('story'); // true/false 

or even:

$(".page.story").length; // 0/n 

additionally, if wanted result of conditional happen after .load has completed, must move inside .load callback after animation line.

$('.content_loading_container').load('content/'+filename, function(){     $('.content_wrapper').delay(250).animate({opacity:1}, 500);     if ($('.page').hasclass('story')) {         ... }); 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -