2

I have been following the Kevin Luck example of the javascript css switcher. http://www.kelvinluck.com/assets/jquery/styleswitch/index.html

I got it to work on my local projects. My problem / question is, when I use the old javascript Kevin Luck uses, it works. When I try to update the jQuery version, it will break. How can I update this example to use the newest 1.10.2 jQuery?

The index page looks like this (relevant part)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Public Identity</title>

    <link rel="stylesheet" type="text/css" href="css/collegeStyles.css" title="college" media="screen" />
    <link rel="alternate stylesheet" type="text/css" href="css/corporateStyles.css" title="corporate" media="screen" />

    <!-- Google CDN jquery -->
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
    <!-- // <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> -->
    <!-- css style switcher -->
    <script type="text/javascript" src="styleswitch.js"></script>

jQuery (styleswitcher.js) looks like this

/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/

(function($)
{

    $(document).ready(function() {
        $('.styleswitch').click(function()
        {
            switchStylestyle(this.getAttribute("rel"));
            return false;
        });
        var c = readCookie('style');
        if (c) switchStylestyle(c);
    });

    function switchStylestyle(styleName)
    {
        $('link[@rel*=style][title]').each(function(i) 
        {
            this.disabled = true;
            if (this.getAttribute('title') == styleName) this.disabled = false;
        });
        createCookie('style', styleName, 365);
    }
})(jQuery);
// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
function eraseCookie(name)
{
    createCookie(name,"",-1);
}
// /cookie functions

The only relevant css is the style sheet names, which are referred to as college and corporate. You can see them in the php / html code above. I'm pretty confused as to why this would break, and am not sure how to fix. Any ideas would be really helpful. Thanks!

camdixon
  • 852
  • 2
  • 18
  • 33
  • 2
    are there any errors in the console window? – haz0rd Aug 28 '13 at 22:31
  • Yes, Uncaught Error: Syntax error, unrecognized expression: link[@rel*=style][title] jquery.js:1850 at.error jquery.js:1850 mt jquery.js:2460 kt jquery.js:2847 at jquery.js:1289 x.fn.extend.find jquery.js:5730 x.fn.x.init jquery.js:197 x jquery.js:63 switchStylestyle styleswitch.js:22 (anonymous function) styleswitch.js:17 c jquery.js:3048 p.fireWith jquery.js:3160 x.extend.ready jquery.js:433 q – camdixon Aug 28 '13 at 22:48
  • It looks to me it is related to this line in particular: $('link[@rel*=style][title]').each(function(i) – camdixon Aug 28 '13 at 22:51

1 Answers1

3

change

$('link[@rel*=style][title]').each(function(i) 

to

$('link[rel*=style][title]').each(function(i) 

from jQuery documentation:

In jQuery 1.3 [@attr] style selectors were removed (they were previously deprecated in jQuery 1.2). Simply remove the “@” symbol from your selectors in order to make them work again.

Neil S
  • 2,304
  • 21
  • 28
  • Did this, and it worked right away! I tried removing both the @ and the * but that didn't work. So, what does the * do? Thanks so much! – camdixon Aug 28 '13 at 22:57
  • 1
    The * is a wildcard selector so that it will match any `link` element with a `rel` attribute containing 'style' – Neil S Aug 28 '13 at 22:58