I have some stylesheets but how can I let a user save a custom stylesheet from the last time they were on the website? Could I do it in Jquery and HTLM5?
-
depends... are you fine with them totally deleting it at some point (clear localstorage) – Daemedeor Oct 22 '15 at 04:26
-
@mevius i wouldnt consider this a dup of anything, unless you can specifically find one thats asking how to save stylesheets, thats what this is about, style sheet selection, not cookies... feel free to del this comment once read. – blamb Oct 22 '15 at 05:20
-
i would also disagree its a dupe at least of the one that its potentially being a dupe of – Daemedeor Oct 22 '15 at 05:21
-
@Akidus See http://stackoverflow.com/questions/29986657/global-variable-usage-on-page-reload/ – guest271314 Oct 22 '15 at 05:44
-
@rnevius, it can't be a dupe of a question about setting/getting cookies when the OP is not asking anything at all about cookies. – Sparky Oct 22 '15 at 20:54
-
@Sparky its somewhat related but its not a dupe.. trust me, i've been workng on this nearly 24h >. – Daemedeor Oct 22 '15 at 20:55
-
@Sparky , did you even look at the other thread? – rnevius Oct 22 '15 at 20:56
-
@rnevius, yes, but again, *in this thread*, the user does not request any particular method. I voted to close too, but marked it as "too broad". – Sparky Oct 22 '15 at 20:59
-
@Daemedeor, check my comments again... I'm **not** the one claiming it's a dupe. – Sparky Oct 22 '15 at 21:03
2 Answers
Yes, you can do this. You can either use a cookie, database, or browser storage. If you use browser storage it'd be HTML5, which is pretty simple to accomplish if you know your JavaScript. Otherwise, you have an easy way which is with a cookie, if you have an authentication system, separate that cookie from those which get deleted on logout, so those settings will remain until a user removes their cookies.
Cookies e.g. from MSDN
document.cookie = "test1=Hello";
document.cookie = "test2=World";
var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)test2\s*\=\s*([^;]*).*$)|^.*$/, "$1");
function alertCookieValue() {
alert(cookieValue);
}
e.g. document.cookie = myStyle;
See a full example here How do I create and read a value from cookie?
Then, the more preferable route, HTML 5, which an example is here Local Storage - HTML5 Demo with Code
EDIT: Here is a pastebin that might do it for you(seetrs you in the right direction from where i was coming from). http://pastebin.ca/3211127
-
Cool, I kind of want it fast, easy and less space/code. More of less space/code. – RATIU5 Oct 22 '15 at 04:36
-
-
Well, so my code is not messy on the index.html. Not cluttery with many languages. – RATIU5 Oct 22 '15 at 04:38
-
the least coding, simplest? in your jquery, when they choose the style, in whatever function you have, update storage right there, in this case it the cookie. ill add some snippets to give you an idea what to add. – blamb Oct 22 '15 at 04:38
-
@BrianThomas even better save a class to localstorage that gets applied on load to the root area and then depending on the css on the root, defines the theme that should be applied elsewher – Daemedeor Oct 22 '15 at 04:39
-
I agree @Daemedeor, i think he wanted to do it totally html4, What do you think? I would too use local storage as i mentioned, thats the proper HTML5 way. (ahh, that was my interpretation when he said less space code, where as you asked, much better approach :-) ) – blamb Oct 22 '15 at 04:43
-
@BrianThomas i'm guessing since jQuery is alright to use, its not nessesary to worry too much about html4 compatibility (that's part of the reason why jquery exists after all) but you're sorta right it depends on the stack... i'm assuming since there's no html4 tag its not a concern though ( ik jquery didn't exist to polyfill html4, but its likely when you're using html4, you are more worried about the js) – Daemedeor Oct 22 '15 at 04:45
-
ok, i think what wree getting to here is, what is the real pre-requisite of HTML5 then? Because if you dont care about supporting HTML4 browsers, its not like HTML5 (The JS for it) is any harder to figure out how to use. So is there any other real prerequisites? I dont think there is. Jquery, chrome, ff your good. If so, i would say go HTML5 for sure. – blamb Oct 22 '15 at 04:48
-
@BrianThomas well the question open ends it, there's no requirement for HTML4 (lets be honest here, not too many browsers, can only use HTML4 as the max and almost all of html5 is implemented) the js is not technically tied to html5, but ES5, HTML5 and CSS3 were released around the same time.... but using html5 you get localstorage which is persistent, and probably would have the least amount of code and it doesn't seem as a backend is involved in this or else cookies would be the next best answer – Daemedeor Oct 22 '15 at 04:53
-
I want something that can support most browsers or at least, the more popular ones. – RATIU5 Oct 22 '15 at 04:56
-
right , no prereq basically, except for a browser to support it, and a couple proper tags, and usage of the HTML5 API. I would start with the HTML5 option first @Akidus, and see how far you get, it should be the best solution for you. – blamb Oct 22 '15 at 04:58
-
Do you know the code that could work? Or did you already post that? Let me check. – RATIU5 Oct 22 '15 at 05:00
-
I like this one. Not sure how to change it to styles, not text being saved. http://jsfiddle.net/Benjol/HMEVd/ – RATIU5 Oct 22 '15 at 05:04
-
just save the name of the style only, thats all your concerned wtih, some name. store only that. Then in your javascript, extract the name from storage, then apply that name forward to your dom by altering the css tag using now jQuery, but you need a refresh, so this needs to happen on load so the tag is in place on load, not after. make more sense? – blamb Oct 22 '15 at 05:07
-
@Akidus if you happen to get this resolved, it would be great if you can add points to any comments that were helpful, then we have a good topic here with two answers already. Since there is alot of comments, this helps point out the key helpful comments to you. – blamb Oct 22 '15 at 05:22
-
-
Here is my code. It is all in one piece, though. http://pastebin.ca/3211086 – RATIU5 Oct 22 '15 at 05:30
-
change it like this, http://stackoverflow.com/questions/10334239/how-to-change-css-of-element-using-jquery remove the #sheet idea. my suggestion. but this just covers the css tag itself, remember now they need a refresh, and you need the code to pull from html5 storage still, and in your on click event you need to grab the variable that you created wtih your html5 storage, and add that into the string that you apply to your css. thats just the css tag. then make an on.ready that looks for this storage, and changes the tag immediately, first so their page loads with their style right off. – blamb Oct 22 '15 at 05:54
-
@BrianThomas your paste bin will error, but you're on the right track – Daemedeor Oct 22 '15 at 06:27
-
ok, yeah, i dont like coding in pastebin, too many apps open at the moment already.. :-) – blamb Oct 22 '15 at 06:33
-
I tried the code, do I need to add paths to anything? It hasen't worked yet. Remember, I am adding the js/jquery in a seperate file. Not in the html doc. – RATIU5 Oct 22 '15 at 18:08
-
I am trying this code form pastebin, where exactly do I change the paths and such? – RATIU5 Oct 24 '15 at 04:19
There are many different ways you can do this but if you didn't want to gunk up your server with too much bloat of a database/cookies (this assumes you don't have an authentication system on the backend) then you could save a class so... something like, you'll need to do the css though:
$(document).ready(function () {
applyTheme();
});
$(".theme").click(function (e) {
e.preventDefault();
//removes current stylesheet
$("#customSheet").remove();
//gets the theme name the user clicked
var themeName = $(this)[0].id;
//sets it to storage
setTheme(themeName);
//appends to the head so it loads
$('head').append('<link href="css/' + themeName + '.css" rel="stylesheet" id="customSheet" />');
});
// sets local storage, pretty self explanatory, localstorage is an object that persists across the browser state until the user clears it out, usually because of some other plugin or just clears the cache
function setTheme(theme) {
localStorage.setItem('themeClass', theme);
}
//how to apply the theme to the document's body so that it can conditionally load
function applyTheme() {
var theme = localStorage.getItem('themeClass');
if (theme) {
$('head').append('<link rel="stylesheet" type="text/css" href="css/' + theme + '.css" id="customSheet">');
}
}
so if in your favorite thing, the css file was called "theme1", it'll load theme1 if you have it in the right path
EDIT:
The HTML to accomplish this specific way is
<div id="theme">
<input type="button" id="theme1" class="theme">
<input type="button" id="theme2" class="theme">
</div>
This'll work with all the way back to IE8 with no problems.... http://caniuse.com/#search=localstorage
You can just copy paste this somewhere and it'll work with your current stuff
EDIT: For people wondering what the entire code base would look like after here's a pastie: http://pastebin.ca/3212318

- 1,013
- 10
- 22
-
-
Here: https://www.dropbox.com/sh/2oo2v7o6wy2i975/AACyV-uIAAzXg7lztOit91hwa?dl=0 – RATIU5 Oct 23 '15 at 20:59
-
I was also thinking, I may want to have two more, or even more themes. Will the code still work? – RATIU5 Oct 23 '15 at 23:34
-
-
@Akidus i'll get it working for sure, and yes as long as the pattern holds up. coding is all about patterns after all – Daemedeor Oct 24 '15 at 06:05
-
-
@Akidus look at this fiddle: http://jsfiddle.net/Lfc4xy6m/2/ that should be your html exactly and the js part should be your theme.js, don't worry about having event.js at all – Daemedeor Oct 24 '15 at 07:00
-
That's the nicest thing I have ever seen! Thanks. Will this work with more themes? – RATIU5 Oct 24 '15 at 14:47
-