4

I have the following code:

$('#smallcart .plusone').live('click',function(){
   var id = $(this).attr('id');
   articlenr = id.split('_')[1];
});

this works fine in FF, safari, Chrome, however in IE (7 and 8) it throws an error on the split-function (this property or method is not supported by this object).

if I alert the 'id'-variable I get something like plus_5751. (so I want to get the '5751' part) if I do alert(typeof(id)) I get String as an answer...

Maybe somebody can point me to the right answer?

Thx

Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
Ian
  • 43
  • 1
  • 3
  • 1
    possible duplicate of http://stackoverflow.com/questions/1453521/javascript-split-doesnt-work-in-ie – ArK Oct 19 '10 at 08:12
  • @Paniyar: that's not a duplicate. The issue in that question is due to the use of regular expressions to split a string - this question is about splitting a string by another string. – Andy E Oct 19 '10 at 08:14
  • I cannot reproduce it: http://jsbin.com/imoki3 – Kobi Oct 19 '10 at 08:15
  • works fine for me ( [example](http://www.jsfiddle.net/QvNPg/1/) ). How about adding the script tag, and better yet a link to a working page.. – Gabriele Petrioli Oct 19 '10 at 08:15
  • @Kobi - really? I knew it! IE is getting personal with me! Still throwing an error when I try it... :-( – Ian Oct 19 '10 at 09:15

3 Answers3

4

The split works pretty well in IE. The problem is the part left of the equal-sign. It's an object with all input-fields having the name articlenr and therefor IE quits with 'this property or method is not supported by this object' when you're trying to assign a string to it.

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • Yes!!! It works if I put 'var' in front of 'articlenr'. Thank you so much, I hope you and everybody you love, has a wonderful day! – Ian Oct 19 '10 at 09:34
  • 1
    @Ian - if this answered your question, consider clicking the tick to mark it as the accepted answer. – Marc Gravell Oct 19 '10 at 12:00
2

Your code works just fine for me in Internet Explorer - as it should be expected to. The problem must lie elsewhere, perhaps something is overriding String.prototype.split?. You can see a working example of your code at http://jsfiddle.net/AndyE/6K77Y/. The first thing to check for is any Internet Explorer specific code in your scripts.

I would make one small improvement for efficiency. $(this).attr('id'); is pretty much the long winded way of writing this.id. It's slower, because a new jQuery object has to be created and then the attr function has to run. Without it, your code can be compressed more, whilst still remaining very readable, if you like:

$('#smallcart .plusone').live('click',function(){
   articlenr = this.id.split('_')[1];
});
Andy E
  • 338,112
  • 86
  • 474
  • 445
0

Try renaming your variable 'id' to something else. IE doesn't like it when you name things in your scripts the same as items in the DOM.

Never mind, that seems to have not been the issue in this case. I have, however, had issues in the past with IE specific bugs caused by variable names.

Sam Dufel
  • 17,560
  • 3
  • 48
  • 51