Results 1 to 2 of 2

Thread: Changing stylesheets across multiple pages using javascript

  1. #1
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Changing stylesheets across multiple pages using javascript

    I'm trying to allow the user to change the stylesheet of my website. I can change the stylesheet on a page by page basis (eg. from red style to blue style) but once the user navigates to a different page the stylesheet resets back to the original setting.

    For example, when the user visits the site first its set to the red style, they can change it to blue but when they navigate to another page the style goes back to red again where as I want the style to stick across pages.

    HTML

    Code:
    <html>
    <head>
    <link href="red.css" rel="stylesheet"
    type="text/css" title="red" media="screen" />
    <link href="green.css" rel="alternate stylesheet"
    type="text/css" title="green" media="screen" /> 
    <link href="blue.css" rel="alternate stylesheet"
    type="text/css" title="blue" media="screen" />
    
    <script type="text/javascript">
    function changeStyle(title) {
    var lnks = document.getElementsByTagName('link');
    for (var i = lnks.length - 1; i >= 0; i--) {
    if (lnks[i].getAttribute('rel').indexOf('style')> -1 && lnks[i].getAttribute('title')) {
    lnks[i].disabled = true;
    if (lnks[i].getAttribute('title') == title) lnks[i].disabled = false;
    }}} 
    </script>
    
    
    </head>
    <body>
    <a href = "home1.html">Page 1</a>
    <br>
    <br>
    <a href = "home2.html">Page 2</a> 
    <br>
    <br>
    <a href = "home3.html">Page 3</a> 
    <br>
    <br>
    <br>
    <br>
    <a href = "#" onclick="changeStyle('red')">Red Stylesheet</a>
    <br>
    <br>
    <a href = "#" onclick="changeStyle('blue')">Blue Stylesheet</a> 
    <br>
    <br>
    <a href = "#" onclick="changeStyle('green')">Green Stylesheet</a> 
    </body>
    </html>
    CSS

    Code:
    body
    {
    background-color:red;
    }

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    IanOLeary1 (07-13-2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •