![]() |
Add class to body to randomize background via CSS
Hey all, I was hoping to get some help with this one. How would I add a class (which will be randomized) to my body tag if the initial class is either "templateCollection" or "templateProduct"?
So for example, IF my body tag looks like this: Code:
<body class="templateProduct">Code:
<body class="templateProduct randomizeBackground">I tried using the following type of JS, but I can't seem to get anywhere with it: Code:
<script type="text/javascript">document.getElementsByTagName('body')[0].className+=' randomizeBackground'</script> |
Your script should have worked. Well, except it would do it to all pages, no matter what the initial class name.
But only if you put it *AFTER* the </body> tag in your page. Try this: Code:
</body> |
Only head and body tags are allowed directly inside the html tag. The script tag should actually come immediately before the </body? tag not after it. You can reference the body tag from there even though the </body> tag hasn't been read yet at the time the script starts loading - as it definitely will have been read by the time the script starts running.
|
Interesting. I recall *YEARS* ago having trouble with MSIE (probably MSIE 4, but maybe 5) when I tried to alter the <body> before the </body> tag. Whereas putting the script after the </body> worked.
But I'll readily believe that was a "once upon a time" MSIE quirk. |
Ok that would explain why FFox was highlighting the script tag bright red in my source code - I'll move it and try again at work tomorrow. Right now it is outputting the right value when I set that variable to be displayed in an alert ("templateCollection"), so that's a step in the right direction.
But my new class is still not getting added in. Baby steps - this is further than I got earlier today so I'm happy. Any further suggestions would be appreciated. Thank you both. |
Quote:
|
Ok, so the script does run and output the class name in text correctly - so that's good. However, I can't get an additional class tag added for some reason.
Earlier today someone in the Shopify Forums suggest I try a jQuery snippet as follows: Code:
<script type="text/javascript">http://lmktechnologies.myshopify.com/ Pass: "rofrur" Any ideas would really be appreciated. Thank you. |
It has to be some bonehead move on my part, like a " instead of a ' or something... here are 2 very minimal html docs, with the suggested jQuery and JS options, and I can't seem to get either of them to work and add a class to the body tag.
Javascript EX: http://www.lmktechnologies.com/TEST/...ipt-test1.html JQUERY EX: http://www.lmktechnologies.com/TEST/jquery-test1.html Hopefully this helps us narrow it down... my brain is broken... |
| All times are GMT +1. The time now is 10:48 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.