最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does IE 8 not support push? - Stack Overflow

programmeradmin1浏览0评论

I am trying to insert an key/value pair into a serializeArray(from jquery).

So I have something like

var form = $('#form');
var sendFormData = form.serializeArray();
sendFormData.push({ "name": "Name", "value": "test"});

In firefox this works yet in IE 8 I get

Line: 51 Error: Object doesn't support this property or method

So it seems to be pointing to this line. So does ie 8 not support push if so what is a way I can add a key/value pair that will work in all browsers(the 5 mains ones firefox, ie8, chrome, opera, safari)

I am trying to insert an key/value pair into a serializeArray(from jquery).

So I have something like

var form = $('#form');
var sendFormData = form.serializeArray();
sendFormData.push({ "name": "Name", "value": "test"});

In firefox this works yet in IE 8 I get

Line: 51 Error: Object doesn't support this property or method

So it seems to be pointing to this line. So does ie 8 not support push if so what is a way I can add a key/value pair that will work in all browsers(the 5 mains ones firefox, ie8, chrome, opera, safari)

Share Improve this question asked Jul 18, 2010 at 20:01 chobo2chobo2 85.8k207 gold badges549 silver badges861 bronze badges 2
  • 2 You accepted an answer but didn't explain why your code was breaking... I'm curious! – Ruan Mendes Commented Mar 11, 2011 at 23:28
  • I had a similar problem in IE8 that turned out to be a misleading error message and stack trace. You have to trace all the way into IE8 code, or it can do some irritating things... – Chris Jaynes Commented Jun 28, 2012 at 4:36
Add a comment  | 

5 Answers 5

Reset to default 11

What you have works (even in IE8), you can test it here: http://jsfiddle.net/ZAxzQ/

There must be something outside the question that you're doing to get that error :)

.push() has been around as long as the Array object, I've never seen a browser that doesn't support it...your unsupported error has to be coming from something else.

This is not an exhaustive answer as it won't solve your problem, but the Array.push() method works in IE8:

var arr = [];
arr.push({ "name": "Test Name", "value": "Test Value"});
alert(arr[0].name);    // Displays "Test Name"

The above can also be re-written as follows:

var arr = [];
arr[arr.length] = { "name": "Test Name", "value": "Test Value"};
alert(arr[0].name);    // Displays "Test Name"

I haven't got access to IE atm, but I'm sure it does support push. Check that sendFormData is considered an array:

Object.prototype.toString.call(sendFormData) === '[object Array]';

Something else IE likes to do, is tell you there is an error on the line after the error occurred, so it may be part of the form.serializeArray() line.

I thought I had the same problem; but ended up finding that my problem was that IE7-IE8 didn't implement Array.prototype.indexOf. If you want to use that, though, you can go to this link: indexOf.

Of course, the easiest another solution is to do something like this:

var sendFormData = $("#form").append("<input id='someuniqueID' type='hidden' name='name' value='test' />").serializeArray();
$("#someuniqueID").remove(); //optional could keep it in there if you wanted
发布评论

评论列表(0)

  1. 暂无评论