Click here to Skip to main content
15,886,802 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi Guys,

I am working on a portal which has two option as links-
1) high contrast color
2) reset to default
For this I have made two css-- one for default colors and other for high contrast colors.
I want when a user click on any option that option will work for all the pages till he either logs out or click the other option.
Can anyone tell me how can I achieve this.
I am just looking for the local storage stuff of html, but I am not getting how to implement it. Thanks in advance.
htmlcode-
  • High Contrast
  • Reset to default colors
  • function changeColors(newCSS)
    {
    document.getElementById("currentCSS").href = newCSS;
    }
    Posted
    Updated 9-Jan-15 9:47am
    v2
    Comments
    Sergey Alexandrovich Kryukov 8-Jan-15 13:40pm    
    After some thinking and discussing with OP who clarified the problem in out discussion in comments to my Solution 1, I up-voted this question with 5.
    —SA

    And how this can be related to local storage or sessions?

    [EDIT #2]

    Actually, after OP raised the valid and practically important problem of applying style switch to other pages using only the client-side techniques, I can see that local and/or session storage using respective HTML5 feature could be extremely useful. Please see the discussion in comments. If needed, I would write some prototype illustrating the technique I suggested, but it would take more time. I hope the idea is easy to understand from my comment in the comment section below.

    One issue it to choose between local and session HTML5 storage. Local storage will keep the chosen style switch for a long time (until the user cleans up local data, the procedure depending on particular browser), but the session storage will remember it only during the session, so you should take care to start with default is session data is missing.

    [END EDIT #2]

    It should be a purely client-side issue, very simple. Have different sets of all CSS classes, default and high-contrast. It does not matter if they are in different CSS files on not (or CSS is all embedded in HTML), it's only important to give them different names.

    On user's click, handle it in Javascript and replace all default classes with high-contrast classes, or visa versa, for all elements involved. I think the most convenient way to implement this would be jQuery. It already has all methods you need, and also it allows to get the whole set of elements and do some operation on the set at once. The jQuery wrapper for the element or a set of elements can be obtained using jQuery selectors: by name, id, HTML elements and/or attributes, different combinations of those, and the like:
    http://api.jquery.com/category/selectors[^].

    To replace/add/remove CSS classes, use these functions:
    http://api.jquery.com/category/css[^],
    http://api.jquery.com/addClass/[^],
    http://api.jquery.com/removeClass/[^],
    http://api.jquery.com/toggleClass/[^].

    If you need to learn jQuery (highly recommended), please see:
    http://en.wikipedia.org/wiki/JQuery[^],
    http://jquery.com[^],
    http://learn.jquery.com[^],
    http://learn.jquery.com/using-jquery-core[^],
    http://learn.jquery.com/about-jquery/how-jquery-works[^] (start from here).

    [EDIT #4]

    The solution described about turned out more complicated than it can be.
    When I tried to develop it, I found much simpler and more elegant solution.
    Here is the implementation shown in my short article, complete with usage sample: Theme Selector Based on HTML5 Local/Session Storage: The Simplest Thinkable One[^].

    [END EDIT #4]

    [EDIT #1]

    Alternative, potentially simpler and more maintainable solutions could be based on some server-side technology. You could post-back an HTTP request on user's click to the server side. The page can be re-written on the server side with only one difference: one CSS file replaced with another CSS file, which could be just one line in HTML code referring to an external entity (CSS file).

    It would not be as fast as with pure client-side Javascript solution, but such switches are hardly done too frequently. In return, maintainability could be improved.

    Too bad we don't know what server-side technology you use and if you even use any. So, you can use this idea to figure out yourself how to do it with your technology (again, if you use any). If you want to get more help on this option, clarify this matter and ask your follow-up questions.

    [EDIT #1.2]

    My solution shown in the article I already mentioned makes the client-side solution doing exactly the same, in a very simple way:
    Theme Selector Based on HTML5 Local/Session Storage: The Simplest Thinkable One[^].

    The only problem is that the client should have relatively modern Web browser supporting Web storage, but this is not a problem at all, because this feature is not even an HTML5 feature anymore; even the Web browser poorly supporting HTML5 support Web storage.

    [END EDIT #1.2]
    [END EDIT #1]

    —SA
     
    Share this answer
     
    v7
    Comments
    ssharma21 8-Jan-15 12:57pm    
    Hi Sergey, Thanks for providing solution. but can u please elaborate your answer as I am not that gud in jquery, just going through your highly recommended links. I am using Razor engine to generate views. moreover i dont want to do this at server side, as its just waste of bandwidth and not that fast. I want to know how can I choose a selector that will work for all pages in a site.?
    Sergey Alexandrovich Kryukov 8-Jan-15 13:34pm    
    The best way to elaborate it is starting to read from the link in marked as "(start from here)". Not doing i at server side (you are perfectly right, about some waste of bandwidth) is exactly the point for the first solution (before [EDIT]). This way, you can get the very best performance, at the expense of some more CSS and Javascript work, which is not a big deal. jQuery helps to get it much easier and more reliable.

    All pages on the side? Good point. Sorry, I did not address this issue. No, selectors are unrelated here; they only take DOM objects on a single page: Now I see; this is a good reason to use HTML5 local or session storage. The only problem is when you switch the styles on one page, but want that it was affect other pages.

    All you need to pass through the storage is the object representing of the "class of style". Here is what I suggest: for these objects, assign some strings which would be uses as prefixes for the names of CSS classes. Say, all classes could come in two variants: default* and highContrast*. When the user clicks on certain style, put the string, either "default" (which should be already there on any page) or "highContrast" or more. The style switching script should append (or prepend) this string to all predefined style names and set the styles using jQuery. The minimal code demonstrating this technique would take some time, but is something is unclear, please ask. It turns out to be a pretty neat client-only solution, thank you for raising this problem.

    —SA
    ssharma21 8-Jan-15 14:25pm    
    thanks Sergey for getting my point exactly. but now can u help me by providing some basic coding tactics as i am not at all getting from where to start to implement this.
    Sergey Alexandrovich Kryukov 8-Jan-15 15:21pm    
    As I say, this will take considerable time. If you want it, I can develop some basic samples (it should contain at least 4 files: 2 HTMLs, one shared CSS and shared js). In the meanwhile, could you ask your questions on what's not clear and do some study on the elements of this solution?
    —SA
    Sergey Alexandrovich Kryukov 8-Jan-15 19:38pm    
    A little more patience. I already created code, writing the text.
    It turned out much better than I suggested before: you won't need to have multiple classes for the same elements, you will be able to simply switch CSS files using only the client side, local or session storage. The solution comes out amazingly simple. Just wait a bit.
    —SA
     
    Share this answer
     

    This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



    CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900