Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 613893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:03:10+00:00 2026-05-13T18:03:10+00:00

I’ve written this code that iterates over all global style sheet rules and stores

  • 0

I’ve written this code that iterates over all global style sheet rules and stores them in an array/object. I use this dictionary-like object later to change global rules rather than setting styles on individual elements.

Following code breaks in IE8 but works fine in Firefox3.7 and Chrome4.

var allRules;

$(function() {
    var fileRules;
    allRules = [];
    $.each(document.styleSheets, function() {
        // get rules for any browser (IE uses rules array)
        fileRules = this.cssRules || this.rules;
        $.each(fileRules, function() {
            allRules[this.selectorText] = this;
        });
    });
});

I get Invalid procedure call or argument error. When I try to debug it, this code sucessfully iterates through two CSS style sheet files with rules but when the second one’s iteration is done, it fails.

I can’t seem to find an error in this code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:03:10+00:00Added an answer on May 13, 2026 at 6:03 pm

    The problem

    After thorough testing I found out that document.styleSheets isn’t a regular array in IE. That’s why it breaks in $.each() call when it reaches the end.

    If we take a look at jQuery function itself it has a for loop to iterate over an object that has a length property, falsely believing it’s an array. document.styleSheets does have length property, but it’s obviously not an array. So when this for loop in $.each() is executed:

    for (var value = object[0];
         i < length && callback.call( value, i, value ) !== false;
         value = object[++i]){}
    

    it fails after the last element has been iterated over. As we may see this for loop doesn’t increment i on its own but rather increments it while assigning a new value to value.

    We can check this manually as well. Write these two lines in any browser’s address bar:

    javascript:var a=[1,2,3];alert(a[3]);void(0);
    javascript:alert(document.styleSheets[document.styleSheets.length]);void(0);
    

    The first one runs fine in all browsers, but the second one fails in IE.

    The solution

    We have to rewrite the iteration over style sheets

    var allRules;
    
    $(function() {
        var fileRules;
        allRules = {};
        // can't use $.each() over document.styleSheets because it's not an array in IE
        for (var i = 0; i < document.styleSheets.length; i++)
        {
            fileRules = document.styleSheets[i].cssRules || document.styleSheets[i].rules;
            $.each(fileRules, function() {
                allRules[this.selectorText] = this;
            });
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Anything in your application that isn't tested is most likely… May 13, 2026 at 9:20 pm
  • Editorial Team
    Editorial Team added an answer httplib is for the job, and you may refer to… May 13, 2026 at 9:20 pm
  • Editorial Team
    Editorial Team added an answer Delete the branch, then recreate it: $ git branch -D… May 13, 2026 at 9:20 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.