PDA

View Full Version : combining an if statement with other javascript



rich1234
11-01-2007, 11:25 AM
Hi, Hope someone can help and indeed if it can be done.

I need to combine this if javascript


<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>")
}
else if (time>=10 && time<11)
{
document.write("<b>Good day</b>")
}
else
{
document.write("<b>Hello World!</b>")
}
</script>


with this.


<a onmouseover="doTooltip(event,'<img src=&quot;<%=Server.URLPathEncode(rsCard("ThumbnailURL"))%>&quot;>

<div class=&quot;tp2&quot;><%If Trim(rsCard("Author"))<>"" Then%> <%=rsCard("Author")%><%End If%></div>' )" onmouseout="hideTip()" href="http://xxxxxxx">xxxxxxx</a></font>

I basically need the onmouseover event which has an asp include to display a different image at different times. I have three asp includes ABC so I am imagining something along the lines of.


document.write("<a onmouseover="doTooltip(event,'<img src=&quot;<%=Server.URLPathEncode(rsCard("ThumbnailAURL"))%>&quot;>")
}
else if (time>=10 && time<16)
{
document.write("<a onmouseover="doTooltip(event,'<img src=&quot;<%=Server.URLPathEncode(rsCard("ThumbnailBURL"))%>&quot;>")
}
else
{
document.write("<a onmouseover="doTooltip(event,'<img src=&quot;<%=Server.URLPathEncode(rsCard("ThumbnailCURL"))%>&quot;>")
}

I know thats completely wrong, but hope someone gets the idea. It of course needs to include the rest of the code.
Any help would be appreciated.
Thanks
Richard

djr33
11-01-2007, 12:13 PM
ASP, PHP, and any other server side code is processed at the time the page is served; there is no ASP, etc. left in th source, in fact. So this means, in terms of javascript, it's just like having the url typed directly there, assuming it is in fact output correctly by the ASP.

The simple answer is to use a function.

function name() {
...
}

call name() in the onMouseover, and then place the if statements in the function (which would go in the script tags, not in the onmouseover attribute).

rich1234
11-01-2007, 04:21 PM
Daniel, Thanks for the quick reply. It makes sence. I will try and do what you suggest.
Thanks again
Richard

Trinithis
11-01-2007, 04:39 PM
else if (time>=10 && time<11)
I would like to point out that time is an integer (due to getHours()), so you could and should write the above snippet as
else if(time==10)