Forum Moderators: open

Message Too Old, No Replies

Can't figure out how to edit this code to get desired result

Should be simple, but I can't seem to get it

         

MatthewHSE

3:28 pm on Feb 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm working on implementing changable stylesheets as discussed at [alistapart.com ]. In a nutshell, JavaScript is used to switch between various alternate stylesheets. A cookie is set to remember what stylesheet had been selected, so it will still be selected on future visits to the page.

I want to include one "printer friendly" stylesheet that, when selected, will NOT set or change any cookies. The idea being that a user would select "printer friendly" only to print, and would not want that preference remembered.

Anyway, I've been monkeying around and can't figure out how to edit the JavaScript to do what I want it to do. Here's the code:

function setActiveStyleSheet(title) { 
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style")!= -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
}

function getActiveStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style")!= -1 && a.getAttribute("title") &&!a.disabled) return a.getAttribute("title");
}
return null;
}

function getPreferredStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style")!= -1
&& a.getAttribute("rel").indexOf("alt") == -1
&& a.getAttribute("title")
) return a.getAttribute("title");
}
return null;
}

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 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;
}

window.onload = function(e) {
var cookie = readCookie("style");
var title = cookie? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
}

window.onunload = function(e) {
var title = getActiveStyleSheet();
createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

Does anyone have any ideas on how to get that one stylesheet to NOT be remembered on later visits?

Thanks,

Matthew

Purple Martin

10:38 pm on Feb 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add an if statement to the start of the createCookie function that exits the function if the style matches the name of your print stylesheet (I've called it "printStyles" here):

function createCookie(name,value,days) {
if (value=="printStyles") return;
...etc