AZZA-KHAMEES
asked on
How to Use JavaScript to Change a Cascading Style Sheet (CSS) Dynamically
Hi Experts, i found a very code example in the internet on how to change the stylesheete in java script using multiple css, i attached my code.
there are 2 problems with this code:
1- in the example i am using cookies to save the user selection but its not working, in each page the style return to its default even if the user choose another style? is something wrong with the javascripts functions?
2- i am getting an error in the internet explorer for the javascript
Error: '2' is null or not an object
and this error belong to this function
""" get_cookie ( cookie_name ) """ for this line "" return decodeURIComponent ( cookie_value[2] ) ""
Can you please help me solving this problem? coz i am still cant understand cookies
Thank you.
there are 2 problems with this code:
1- in the example i am using cookies to save the user selection but its not working, in each page the style return to its default even if the user choose another style? is something wrong with the javascripts functions?
2- i am getting an error in the internet explorer for the javascript
Error: '2' is null or not an object
and this error belong to this function
""" get_cookie ( cookie_name ) """ for this line "" return decodeURIComponent ( cookie_value[2] ) ""
Can you please help me solving this problem? coz i am still cant understand cookies
Thank you.
Hi! Could you please post the code?
ASKER
i attached the example
Example.txt
Example.txt
Ok, the javascript error is the showstopper. I guess cookie_value is no array :-)
Replace the function:
Replace the function:
function get_cookie ( cookie_name )
{
// http://www.thesitewizard.com/javascripts/cookies.shtml
var cookie_string = document.cookie ;
if (cookie_string.length != 0) {
var cookie_value = cookie_string.match (
'(^|;)[\s]*' +
cookie_name +
'=([^;]*)' );
return decodeURIComponent ( cookie_value) ;
}
return '' ;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
generally speaking, javascript will NOT allow you to access the contents of the HEAD of a document. Once you realize it is only BODY accessible, then you can figure out what you need to do to get it to work.
The code works, check here:
http://bluv11t.bravehost.com/JS_change_CSS.html
http://bluv11t.bravehost.com/JS_change_CSS.html
ASKER
Thank you, its really worked Thank you bluV11t :)