Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: MiXailo - lokakuu 17, 2013, 03:18:21 IP

Otsikko: [HELP] JavaScript changing background of header.
Kirjoitti: MiXailo - lokakuu 17, 2013, 03:18:21 IP
SMF Version : 2.0.5
Problem : I have made javascript code, and I implement it to my theme ( Vivanto ), but it isn't working! So, help me  :)
JS Code :
<script type="text/javascript">
var body = document.getElementById("header");
var datum = new Date();
var vreme = datum.getHours();
if(vreme >= 7)
{
body.style.backgroundImage = "url("../images/custom/dnevna.jpg")";
     }
     else if(vreme >= 19)
{
body.style.backgroundImage = "url("../images/custom/nocna.jpg")";
}
</script>

Thanks very much !
Otsikko: Re: [HELP] JavaScript changing background of header.
Kirjoitti: Shambles - lokakuu 17, 2013, 04:12:07 IP
Have a read of this topic - a very similar request and a very similar resolution (regarding the use of quotes):


http://www.simplemachines.org/community/index.php?topic=512655
Otsikko: Re: [HELP] JavaScript changing background of header.
Kirjoitti: kat - lokakuu 17, 2013, 04:17:27 IP
(https://www.simplemachines.org/community/proxy.php?request=http%3A%2F%2Fwww.katzy.dsl.pipex.com%2FSMF%2Fwelcome.gif&hash=64d0bdab41bf056c5af638442124afd2cc58a5fb)

For future reference... When you post, asking for help, saying "It isn't working" isn't, really, terribly useful.

If you're getting errors, please tell us what the errors are. The more information that you can give us, the better chance we have of helping you. :)
Otsikko: Re: [HELP] JavaScript changing background of header.
Kirjoitti: butch2k - lokakuu 18, 2013, 01:47:26 AP
i suspect "url("../images/custom/dnevna.jpg")" and its brother to be the culprits...
note the double quotes positions, try to replace the inner " by \' (escaped since they are probably outputted through an echo).
Otsikko: Re: [HELP] JavaScript changing background of header.
Kirjoitti: Arantor - lokakuu 18, 2013, 08:01:17 AP
Nah, there's other quotes there that should be fine.

Seems to me the problem is the pathing; given the situation where this is called, I'd argue it shouldn't have the ../ at the start.
Otsikko: Re: [HELP] JavaScript changing background of header.
Kirjoitti: MrPhil - lokakuu 18, 2013, 09:32:31 AP
What happens between the hours of 0 and 6? Consider
else if(vreme >= 19 || vreme < 7)

The " inside of a "-delimited string should certainly be a problem -- as mentioned before, change the inner to ' (\' if the whole thing is output with ' delimiting).
Otsikko: Re: [HELP] JavaScript changing background of header.
Kirjoitti: butch2k - lokakuu 18, 2013, 04:03:51 IP
Lainaus käyttäjältä: Arantor - lokakuu 18, 2013, 08:01:17 AP
Nah, there's other quotes there that should be fine.

Seems to me the problem is the pathing; given the situation where this is called, I'd argue it shouldn't have the ../ at the start.
Hummmm...
"string1"string2"string3" won't cut it in JS. He uses the same string delimiters for both the JS string AND the url() content string, it won't work. He may drop the inner " or replace them by \' or \" but keeping the " for the inner string won't work.
You'll get a "SyntaxError: Unexpected token ." for sure.
Otsikko: Re: [HELP] JavaScript changing background of header.
Kirjoitti: Arantor - lokakuu 18, 2013, 04:13:59 IP
Oh, I see what you're getting at now. The fact the URL's almost certainly wrong is just a bonus. (In my defence I'd only been up 10 minutes when I saw this earlier, not awake yet)