We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10290
    • 19 Posts
    Hi, I’m using styleswitcher.js in my 0.9.5 MODX site to let the user choose the css and it works well but when you go to a new page you can first see the default css and then it turns to the selected one, I don’t like this blink at all, is there a way to get it out or is there a way to alternate css better??

    Thank you
      • 26439
      • 104 Posts
      I spent a while working on this "hack" version of swapping stylesheets determined by user input.....

      http://modxcms.com/forums/index.php/topic,11596.0.html ( third post for description of the working version )
        • 10290
        • 19 Posts
        Thank you! I haven’t implemented your solution but its mostly the same.
        What I’d done is this:

        -Create a snippet "[[phpCSSSWitcher]]"
        <?php
        if(isset($_GET[user_style])) // if user_style has been requested by the user in the url
        {
              $user_style = $_GET[user_style]; // define user_style
              setcookie("user_style",$_GET[user_style],time() + (86400 * 30), "/", "", 0); // set cookie for 30 days
        }else  // else if there is no user_style variable in the url
        {
           if(isset($_COOKIE['user_style'])) // see if there is a cookie
              {
              	$user_style = $_COOKIE['user_style']; // if so set user_style to cookie user_style
              }else{
              	$user_style = 0; // if not use default
              }
        };
        
        $path = '<link rel="stylesheet" href="[(base_url)]assets/templates/css/';
        switch($user_style){
        	case 0:
            	   $path = $path.'site1.css';
                   break;
        	case 1:
        	   $path = $path.'site2.css';
                   break;           
        }
        /** Complete the link and echo its html code **/
        echo $path.'" type="text/css"/>';
        ?>


        -Create the links to let the user choose the template. For this I have a chunk called at the top of all documents
        <a id="selectEstilo" href=[~[*id*]~]?user_style=0>Oscuro</a>
        <a id="selectEstilo" href=[~[*id*]~]?user_style=1>Claro</a>


        - Between the tags <head> of the template I call the snippet "[[phpCSSSWitcher]]"

        As you can see is the same of yours, but grouped in just one snippet.
        Hope this help others, and thanks again!! wink

        NOTICE: Change the paths and styles to your fits
          • 26439
          • 104 Posts
          Did that snippet work btw? I’m having trouble getting a snippet to access the user_style via...

          $_GET[user_style]
          
            • 13094
            • 58 Posts
            funkafize’s Snippet will only work for me when using uncached Snippet Call [! ... !]

            Cheers