Grow your CSS skills. Land your dream job.

Code made for jQuery 1.4 not working in jQuery 1.8.3

  • # January 1, 2013 at 1:45 am

    I was following Chris’s “Dynamic Page/Replacing Content” guide (http://css-tricks.com/dynamic-page-replacing-content/), and everything was working fine. Then I added jQuery for something else on my site and I had to use version 1.8.3 (the latest one). Chris’s tutorial tells you to use version 1.4 (because the tutorial is from 2010).

    Anyway, these couple of code lines in the jQuery (straight from Chris’s tutorial) don’t work:

    $(“nav a”).removeClass(“current”);
    $(“nav a[href='"+newHash+"']“).addClass(“current”);

    This code, for some reason, isn’t playing well with jQuery 1.8.3. Was there some sort of language scripting update in jQuery since 1.4 to 1.8.3? I know for a fact that this is the problem. I switched my jQuery link to 1.4 and suddenly this class switching code works perfectly fine, but my other image gallery (for which I need the latest version of jQuery) stops working. So I need to have 1.8.3, but also need to ensure that this class switching code works. I’ve triple-checked with Firebug and this is indeed the problem.

    To add to the confusion, the code from the tutorial doesn’t work at all on Chrome. If you’ve read through it, you’ll see that it adds a hashtag at the end of the url, then fades out the current page content, and fades in the destination page content. Works like a charm on Firefox, absolutely nothing on Chrome. But it works totally fine on Chrome on my Android device. Weird, I know. I cleared out my cache and everything, tested locally and on a web server, yet desktop Chrome won’t listen.

    But does anyone know anything about the jQuery 1.4 to 1.8.3 problem?
    Any help/tips/advice appreciated!
    Thanks in advance.

    # January 2, 2013 at 1:37 am

    It looks like it’s the [delegate()](http://api.jquery.com/delegate/) method’s fault. It seems it’s been removed from the later jQuery versions. The [on()](http://api.jquery.com/on/) method should be used instead.

    # January 2, 2013 at 11:08 pm

    @jamy_za: Thanks! I changed it to .on(), with the correct parameter order, but it still doesn’t fix the problem. The problem that in the navigation, the link corresponding to the page that the user is on does not get the “.current” class added to it (which that line of code which I posted in my original question should do). I noticed that .bind() should also be replaced with .on(). I did that, but still doesn’t work.

    What other functions in that jQuery code from that tutorial are outdated since 1.4? Sorry, I’m not very experienced with jQuery. I thought this would be a simple copy+paste but apparently a lot has changed with jQuery versions.

    # January 3, 2013 at 12:43 am

    Hmm, ok I’ve downloaded Chris’ dynamic load script and I’ve gotten it to work with jQuery 1.8.3:

    jQuery(function($) {

    var newHash = “”,
    $mainContent = $(“#main-content”),
    $pageWrap = $(“#page-wrap”),
    baseHeight = 0,
    $el;

    $pageWrap.height($pageWrap.height());
    baseHeight = $pageWrap.height() – $mainContent.height();

    $(“nav”).on(“click”, “a”, function() {
    window.location.hash = $(this).attr(“href”);
    return false;
    });

    $(window).on(‘hashchange’, function(){

    newHash = window.location.hash.substring(1);

    if (newHash) {
    $mainContent
    .find(“#guts”)
    .fadeOut(200, function() {
    $mainContent.hide().load(newHash + ” #guts”, function() {
    $mainContent.fadeIn(200, function() {
    $pageWrap.animate({
    height: baseHeight + $mainContent.height() + “px”
    });
    });
    $(“nav a”).removeClass(“current”);
    $(“nav a”).filter(function(){
    return $(this).attr(‘href’) == newHash;
    }).addClass(“current”);
    });
    });
    };

    });

    $(window).trigger(‘hashchange’);

    });

    Let me know if you have any other problems/questions.

    Note: You should run that through some kind of vhost. Don’t just run the index.html file otherwise the ajax won’t run due to cross domain security issues.

    # January 3, 2013 at 5:43 pm

    @jamy_za: Thanks so much!! You’re a lifesaver. It works like a charm on Firefox and IE9, exactly the way I want it to. One last quick question: Do you happen to know of any reason why the jQuery won’t run at all on Chrome? I’m running Fancybox2 as well, and Chrome won’t even run that or this page-fade script. Is it a security issue that’s caused by us messing with the url?

    These are the scripts I’ve got in my head right now:






    First one is simply the jQuery library (calling it without the http so that it works with https). Second and third script are for this page-fade effect. Third and fourth ones are for fancybox2, and the last one just tells it to run the fancybox function once the page loads.

    I uploaded the site to an actual server and checked it, and Chrome won’t run any of the jQuery. It doesn’t even do the hashtag url change, it just goes straight to that page as if there was no jQuery/AJAX. Works perfectly fine in Firefox and IE9 though. Chrome just won’t run the jQuery/AJAX. I don’t need a vhost if I’m accessing the site straight from a web server with the actual url, do I? Do you happen to know why Chrome won’t play nice? Thanks again for all your help.

    # January 4, 2013 at 1:59 am

    Figured out why Chrome wasn’t running in jQuery. I debugged it in console and it said “Origin Null is not allowed by Access-Control-Allow-Origin” on all the jQuery links in the head tag. Looked it up and this happens because apparently Chrome and Safari (and consequently -webkit-) have tighter security controls than Firefox/IE9 do. They won’t let you link locally via AJAX to other files in the site, even if they’re in the root directory.

    The fix to this is to add “–allow-file-access-from-files” to your Chrome target. So, right click on Google Chrome, go to Properties, and paste that at the end of the Target textbox. It will look like this: C:UsersXXX_USERAppDataLocalGoogleChrome Applicationchrome.exe –allow-file-access-from-files.

    Now run it locally and it should work. Note: Remove it after you’re done testing locally. It’s less secure with that script running. Apparently, the jQuery will run if the site is actually hosted on a web server. I’ll test it out and hopefully it’ll work fine on Chrome from any computer, not just mine.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".