0

Possible Duplicate:
window.location = #anchor doesn't work in IE

I have a select box which when clicked changes the hash in the URL for my AJAX based page. I have a function that periodically checks window.location.hash for a change then updates the content accordingly. The following code is the function that changes the URL when the select is changed. It works in Firefox but I cannot get it to work in IE! I keep getting an Object doesn't support this property or method error:

#ob is my select

$("#ob").change(function() {
   ob = $(this).val();
   window.location.hash = "#ob=" + ob;
});

Any suggestions? Thanks!

Community
  • 1
  • 1
user2781234
  • 13
  • 1
  • 3

2 Answers2

1

The problem is not the hash, the error happens in the line before.
declare the variable with the var-keyword:

$("#ob").change(function() {
   var ob = $(this).val();
   window.location.hash = "#ob=" + ob;
});

Read the explanation here: jQuery selector does not work in IE7/8

Community
  • 1
  • 1
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • While good practice, this wouldn't fix the problem. – josh3736 Sep 04 '12 at 03:25
  • Allow me to rephrase: this *shouldn't* fix the problem. I know IE adds every element's ID as a global property, but from what I remembered, overwriting those wasn't a problem. [I did some testing](http://jsfiddle.net/josh3736/zNhwK/), and oddly enough, just assigning to the global `ob` does indeed throw. However, if you first overwrite `window.ob`, then this works fine. – josh3736 Sep 04 '12 at 03:40
1

Rather than rolling your own state management, I'd just go with a battle-tested library to handle everything for you. My favorite is jQuery BBQ.

$("#ob").change(function() {
   var ob = $(this).val();
   $.bbq.pushState({ ob:ob });
});

As an added bonus, BBQ hooks in to modern browsers' onhashchange event rather than polling for changes to location.hash.

josh3736
  • 139,160
  • 33
  • 216
  • 263