1

I have been looking into this issue for the past few days and cannot figure it out. The code below, searches an external file for content based off current page class, then loads content into any matching ID's on the page. It works in Chrome, Firefox, IE9 but recently stopped working in IE8 and I cannot figure out why. Any thoughts would be much appreciated.

HTML looks like this

<body class="jms">
    <div id="mainHomeContent" class="shared"></div>
</body>

jquery running on ready

$("div.shared").each(function(){
        var Body = $(document).find("body");
        var contentID = ("#" + $(this).attr("id"));
        var pathname = ""

        if(Body.hasClass("pigman")){
            var pathname = "/dev/jmsracing/content/pigman/shared-content-include.html"
        } else if(Body.hasClass("marion-arts")){
            var pathname = "/dev/jmsracing/content/marion-arts/shared-content-include.html"
        } else if(Body.hasClass("jms")){
            var pathname = "/dev/jmsracing/content/jms/shared-content-include.html"
            alert('hello');
        }

        $(contentID).load(pathname + " " + contentID);  
    }); 
danronmoon
  • 3,814
  • 5
  • 34
  • 56
trobbins26
  • 219
  • 1
  • 5
  • 13

2 Answers2

2

What i think is he is iterating with same id where ie is very strict about it so this should be the solution:

$(function() {
    var Body = $(document).find("body");
    var contentID = ("#" + $(this).attr("id"));
    var pathname = ""

    if (Body.hasClass("pigman")) {
        var pathname = "/dev/jmsracing/content/pigman/shared-content-include.html"
    } else if (Body.hasClass("marion-arts")) {
        var pathname = "/dev/jmsracing/content/marion-arts/shared-content-include.html"
    } else if (Body.hasClass("jms")) {
        var pathname = "/dev/jmsracing/content/jms/shared-content-include.html"
        alert('hello');
    }

    $(contentID).load(pathname + " " + contentID);
});​
Jai
  • 74,255
  • 12
  • 74
  • 103
1

Try this:

$("div.shared").each(function () {
    //combined into one var statement...not really necessary.
    var $body = $("body"),
        contentId = "#" + $(this).attr("id"),
        pathname = "";

    //you've declared pathname above no need for "var" each time below
    //also added missing semi colons
    if ($body.hasClass("pigman")) {
        pathname = "/dev/jmsracing/content/pigman/shared-content-include.html";
    } else if ($body.hasClass("marion-arts")) {
        pathname = "/dev/jmsracing/content/marion-arts/shared-content-include.html";
    } else if ($body.hasClass("jms")) {
        pathname = "/dev/jmsracing/content/jms/shared-content-include.html";
        alert('hello');
    }
    // $(this) and $(contentId) are the same element 
    // since you are getting the "id" from "this"
    // us $(this) instead
    $(this).load(pathname + " " + contentId);  
});
KTastrophy
  • 1,739
  • 15
  • 23
  • @Jai - he does not redfine pathname 4 times, does not use an inefficient selector for the body and properly passed jslint – Mark Schultheiss Dec 21 '12 at 17:54
  • So it was mainly just cleanup. Thanks for the help. Both are great Answers. In addition i changed $(this).load(pathname + " " + contentId + "Content"); So the loaded div will not have the same name. Thanks again – trobbins26 Dec 21 '12 at 18:31
  • I checked this one since cleanup was suggested. – trobbins26 Dec 21 '12 at 18:32