1

In the following code, I included an attribute for debugging. In FF and Chrome, I get a ton of alerts saying "attribute found", but in IE, I get nothing. The function returns an empty array.

I have also tried removing the console.info(this) line.

BTW, I'm using SPServices to access lists from SharePoint 2010 -- I'm trying to get all the columns of a list.

/*!
 * listAttributes jQuery Plugin v1.1.0
 *
 * Copyright 2010, Michael Riddle
 * Licensed under the MIT
 * http://jquery.org/license
 *
 * Date: Sun Mar 28 05:49:39 2010 -0900
 */

 //THIS ISN'T WORKING IN IE
if(jQuery) {
    jQuery(document).ready(function() {
        jQuery.fn.listAttributes = function(prefix) {
            var list = [];
            var attributes = [];
            $(this).each(function() {
                console.info(this);
                for(var key in this.attributes) {
                alert("attribute found");
                    if(!isNaN(key)) {
                        if(!prefix || this.attributes[key].name.substr(0,prefix.length) == prefix) {
                            attributes.push(this.attributes[key].name);
                        }
                    }
                }
                list.push(attributes);
            });
            return attributes;
        }
    });
}
//end listAttributes Plugin - use this to see what attributes 


function ImportSPListColumnsToArray(ListName)
{
    var ArrayForStorage = new Array();
$(document).ready(function() {


  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: ListName,
    CAMLViewFields: "",
    CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='RecursiveAll'/></QueryOptions>",
    **completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName='z:row']").each(function() {
      //find all fields used by each row and aggregate them without duplicating
      var row_attr = $(this).listAttributes();**
      for (var i=0; i<row_attr.length; i++)
        {
            if ($.inArray(ArrayForStorage, row_attr[i]) == -1)
                {
                    ArrayForStorage.push(row_attr[i]);
                }
        }
      row_attr.clear();
      });
    }
  });

});
  return ArrayForStorage;

}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Christian
  • 552
  • 1
  • 5
  • 12
  • Irrelevant to your question, but just FYI: it's unusual to declare a plugin inside `$(document).ready()`. It's more common to declare it in the global scope. Also when you're using `$(this).each`... `this` will already be a jQuery object, so you can (and should) be using just `this.each` – Matt Aug 12 '11 at 19:11
  • What is `.listAttributes();`? I don't think it's part of the jQuery library. [EDIT] Oops, sorry, I was only seeing part of your code. – bfavaretto Aug 12 '11 at 19:22
  • @bfavaretto that's why Christian has `jQuery.fn.listAttributes = function(prefix) { ... }` – Peter Ajtai Aug 12 '11 at 19:23
  • @Matt - `this` is the DOM element. For example `this.hide()` will not work only `$(this).hide()` will - **Examples** `this`: http://jsfiddle.net/9yxe8/ `$(this)` http://jsfiddle.net/4a6fE/ ------- What is good practice is to cache `$(this)` ==> http://jsfiddle.net/Bjhbc/ – Peter Ajtai Aug 12 '11 at 19:28
  • @Peter: In the case where he's using `$(this).each`, `this` is pointing to the `jQuery` object he called `listAttributes` on: http://jsfiddle.net/Bjhbc/1/ – Matt Aug 12 '11 at 19:38
  • @Matt: taking out the $(document).ready and changing $(this) to this did not resolve the issue. That's good insight, though. – Christian Aug 12 '11 at 20:02
  • @Matt - Thanks, I see what you mean now. – Peter Ajtai Aug 13 '11 at 03:52

2 Answers2

0

I could be wrong, but I believe your problem is the console.info(this); line. IE does not have this available out of the box (you need to open Developer Tools first), so you're script is failing at that point.

Community
  • 1
  • 1
Mike G
  • 4,713
  • 2
  • 28
  • 31
0

Figured it out.

IE apparently doesn't recognize this.attributes as having a length in the for each loop.

So I just iterated through them, and it worked.

if(jQuery) {
        jQuery.fn.listAttributes = function() {
            var attributes = new Array();
            $(this).each(function() {
                for (var i=0; i<this.attributes.length; i++)
                {
                    attributes.push(this.attributes.item(i).nodeName);
                }
            });
            return attributes;
        }
}
Christian
  • 552
  • 1
  • 5
  • 12