2

I am trying to extract the media query content css properties using JS. However quotation marks seem to be stripped. See the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript">

    $(window).load(function() { 

        for(var i = 0; i < document.styleSheets.length; i++){

            for(var j = 0; j < document.styleSheets[i].cssRules.length; j++){

                for(var k = 0; k < document.styleSheets[i].cssRules[j].cssRules.length; k++){

                    var css_text = document.styleSheets[i].cssRules[j].cssRules[k].cssText;

                    console.log('css_text');
                    console.log(css_text);

                }

            }

        }

   });

</script>
<style type="text/css">

    @media screen and (max-width: 960px) {

        body:after{
            content: "hello";   
        }

    }

</style>
</head>
<body style="margin: 0px;">

   some stuff here...

</body>
</html>

The above returns:

css_text
body::after { content: hello; }

The return i hoped for:

css_text
body::after { content: "hello"; }

I am using the Google Chrome browser.

Is there any way i can possible preserve the quotation marks that originally surrounded the hello?

Bilbo Baggins
  • 95
  • 1
  • 9

1 Answers1

1

You need to escape the quotes. The following link is one way to do it.. http://magp.ie/2011/01/20/addslashes-and-stripslashes-in-javascript/

So you would instead write
console.log(addslashes(css_text));

Let me know if it works :)

asifrc
  • 5,781
  • 2
  • 18
  • 22
  • Hi, i tried that just now but sadly it does not work. Thanks for your help though! – Bilbo Baggins Feb 04 '13 at 10:07
  • I'm on my tablet atm and about to sleep, but I'll give it a stab in the morning if this is still active :-) – asifrc Feb 04 '13 at 10:09
  • 1
    Tried playing around with it in chrome. It seems like there's a a fairly heavy duty function built in that removes/escapes the css before you're even allowed to access it. For instance, removing hello and just leaving "" will show content: ''. And then replacing "hello" with url('this.html?p="param") will return an absolute url and automatically url encode the quotes. Unfortunately, beyond noting that there's some dark and mysterious force that changes your values before you display them, I don't know much more about how to fix the problem :/ – asifrc Feb 04 '13 at 18:07