views:

1081

answers:

3

Hi,

I have the following code and its working (as usual) in everything but IE. Its giving me a unexpected call to method or property access in Jquery and I have no idea how to debug it. Ive been using the IE developer toolbar, which is useless for this error and just gives me a line no 12 (inside the jquery script).

Any help is v much appreciated:

<script type="text/javascript">
$(document).ready(function () {

    $.history.init(pageload);

    $('a[href=' + window.location.hash + ']').addClass('selected');

    $('a[rel=ajax]').click(function () {

        var hash = this.href;
        hash = hash.replace(/^.*#/, '');
        $.history.load(hash);

        $('a[rel=ajax]').removeClass('selected');
        $(this).addClass('selected');
        $('.loading').show();

        getPage();

        return false;
    });
});

function pageload(hash) {
    if (hash) getPage();
}

function getPage() {

    hash = document.location.hash;
    hash = hash.replace(/^.*#/, '');
    var data = 'page=' + encodeURIComponent(hash);
    $.ajax({
        url: "index.php",
        type: "POST",
        data: data,
        cache: false,
        success: function (html) {
            $('.loading').hide();
            $('tbody').html(html);

        }
    });
}
</script>

Here is the history plugin: http://plugins.jquery.com/project/history

And here is the demo i have been following: http://plugins.jquery.com/project/history

Still changing window.location back to document.location doesnt seem to make a difference

Im lost on this one. When I change the tag im calling to it does post so its working, but in ie the design is all broken and the next links i click on dont post. Really Strange!!, works fine in FF,opera etc

+1  A: 

I'm a bit surprised IE complains about it, but it's a good thing it does: You're missing a declaration in getPage for hash (e.g., put var in front of the first use).

On the others it's presumably creating an implicit global (a property of the window object called hash), which is of course a Bad Thing(tm), but as I understand it it's correct according to the specification (the relevant sections being 8.7 ["The Reference Type"] and 10.1.4 ["Scope Chain and Identifier Resolution"]).

Still surprised IE is complaining about it, though. It must have to do with the scope in which jQuery is calling your click handler.

T.J. Crowder
ive narrowed it down to the tbody tag. Any idea why this is causing a problem? Does IE not recognise this tag? PS Thx you were right about the things you mentioned as well
Ke
@Ke: IE recognises the `tbody` tag, yes. I've just realized that you were doing `$('tbody').html(html)` -- do your *really* want to write this content to every `tbody` on the page? And what's the HTML you're writing to it? Persumably a series of `tr` elements... IE doesn't much like you mucking about with table rows via `innerHTML`, but I assume jQuery is handling that for you (Prototype does and I don't think jQuery is more backward in that area, but I don't know jQuery well).
T.J. Crowder
A: 

I can't find a history method in the jQuery API so I presume it's either:

  • A third-party plugin.
  • An indirect call to good old window.history object.
  • A custom object you created.

My guess is that IE (well known for polluting the global scope by making everything global) thinks any reference to history actually means window.history and it's getting confused. If it's a custom object, try to rename it into myHistory or anything else.

Álvaro G. Vicario
i did all this and this history plugin seems somewhat broken. also has problems with more than 50 chars in url, think i will have to find another way, cheers anyway :)
Ke
A: 

I think that Mr. Crowder is on to something. As far as I can tell from the jQuery source, if your returned HTML fragment doesn't contain any <script> tags, and doesn't have a couple of other not-too-likely characteristics, then jQuery is probably trying to stuff it into your <tbody> element by using the "innerHTML" property. IE doesn't like that.

What you might have to do is wrap the whole <table> in a dummy <div> and then rebuild the entire thing from your HTML fragment.

Pointy