Forum Moderators: open

Message Too Old, No Replies

calling javascript from rollover

         

Kelt

9:53 pm on Sep 1, 2004 (gmt 0)

10+ Year Member



Hi
I'm new to just about everything, so bear with me.
I have a 'contact' rollover which links to a mailto:
I would like instead to call an external script action which is an email blind, but maintain the rollover functionality.
The rollover looks like this:

<a onclick="changeImages('WUContact','WU%20Button%20BW.gif');" onmousedown="changeImages('WUContact','WU%20Button%20negative.gif');return true" onmouseover="changeImages('WUContact','WU%20Button.gif');return true" onmouseout="changeImages('WUContact','WUContact.gif');return true" ><img id="WUContact" src="WUContact.gif" alt="Email the authors" name="WUContact" height="50" width="100" border="0"></a>
(took the mailto: out)

The Javascript looks like this:

<script type="text/javascript">ewrite("name","domain","com");</script>

and refers to an external js file, and I have

<script src="GeneratedItems/fun.js" language="javascript" type="text/javascript"></script>

in the header.
My question (finally) is: where do I insert
<script type="text/javascript">ewrite("name","domain","com");</script>
In order to keep the rollover working, and also to make the script work? (both work fine on their own, just won't work together). The rollover is inside a table cell.
Thanx a mill

StupidScript

10:54 pm on Sep 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Insert it before your image-swapping reference that needs the "return true" statement:

<a onmouseover="ewrite('name','domain','com');changeImages('WUContact','WU%20Button.gif');return true">

I would also suggest that you rename your image (and ALL of your other web files) to remove the spaces. Although you might think the "%20" ASCII character replacement has no impact, the percentage sign (%) is often a special character in various scripting languages, and could cause problems.

In Javascript, the % sign is the "modulus" operator, for instance. (var1=12; var2=5; var1 % var2 = 2;). Although you have contained the operator by enclosing it within string delimiters (the apostrophes), you might end up getting things confused.

Kelt

12:24 am on Sep 2, 2004 (gmt 0)

10+ Year Member



Hey, SS, Thanks for the fast reply, unfortunately the script doesn't seem to be called in this configuration. Any other ideas?

Bernard Marx

12:02 pm on Sep 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a feeling that incorporating
ewrite
into the mix is going to involve an uncomfortable
document.write
statement - full of escaped quotes etc.

How's about posting the

ewrite
function itself, then we can fiddle it a bit to make it easier to use.

- oh, and get rid of all the spaces in your image file names :)

Kelt

3:59 pm on Sep 2, 2004 (gmt 0)

10+ Year Member



Thanks for the replies so far.
The spaces in the file names go when I finalize the site prior to upload.
The actual script follows - it's a pretty basic way of assembling the email address on the fly.
I just want to be able to call it from within the rollover, but everything I've tried so far has either broken the rollover or fails to call the script

<!--
function ewrite(a,b,c,d){
var mail;
var link;
mail=a+"@"+b+"."+c;
if(d == 0){
document.write(mail);
}
else {
link='<a href="mailto:'+mail+'">'+mail+'</a>';
document.write(link);
}
}
// -->

Bernard Marx

5:11 pm on Sep 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The string concatenation from Beelzebub.

<html>
<head>
<script>
// presumeably an external script
function ewrite(name,domain,topDomain,attrOnly)
{
var mail;
mail="mailto:"+name+"@"+domain+"."+topDomain;
if(attrOnly)
return mail;
else
return '<a href="mailto:'+mail+'">'+mail+'<\/a>';
}

</script>

<script>
document.write
(
"<a href=\""+ ewrite('myname','mydomain','com','*attrOnly')+"\""
+" onclick = \"changeImages('WUContact','WU%20Button%20BW.gif');\""
+" onmousedown = \"changeImages('WUContact','WU%20Button%20negative.gif');return true\""
+" onmouseover = \"changeImages('WUContact','WU%20Button.gif');return true\""
+" onmouseout = \"changeImages('WUContact','WUContact.gif');return true\">"
+"<img id=\"WUContact\" src=\"WUContact.gif\" alt=\"Email the authors\" name=\"WUContact\" height=\"50\" width=\"100\" border=\"0\">"
+"<\/a>"
)
</script>
</head>
<body>

</body>
</html>

It's not possible to incorporate the ewrite function into this without changes. You have other attributes (event handlers), so the whole thing has to go into a doc.write statement. The function itself has been changed to return the string, instead of executing a doc.write itself.

The last arg '*attrOnly' is just a more informative way of entering

true
.

The other problem with this is that, if there's no Javascript, there's no email at all on the page.

Bernard Marx

5:30 pm on Sep 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the moment, I prefer something more along these lines. It's easier, for a start.
The elements are written in HTML as normal - no doc.write(). The href is #. When the link is clicked, before anything else, a function is called that changes the link's href to the desired spec. The href is followed by default after any other statements in the onclick have executed.

One drawback is that the address doesn't show in the status bar (you could put the function call into the mouseover handler too, I suppose).

<html>
<head>
<title>set mail</title>

<script>
function setMail(linkElm, name,domain,topDomain)
{
linkElm.href = "mailto:"+name+"@"+domain+"."+topDomain;
}
</script>
</head>

<body>
<a href="#"
onclick = "setMail(this,'myname','mydomain','com'); changeImages('WUContact','WU%20Button%20BW.gif');"
onmousedown = "changeImages('WUContact','WU%20Button%20negative.gif');return true"
onmouseover = "changeImages('WUContact','WU%20Button.gif');return true"
onmouseout = "changeImages('WUContact','WUContact.gif');return true">
<img id="WUContact" src="WUContact.gif" alt="Email the authors" name="WUContact" height="50" width="100" border="0">
</a>
</body>
</html>

Another benefit here is with JS-disabled browsers.

This time, your link and image are still on the page. The image could then display your email address, or alternatively, something like this, where the href starts off with a different link:

<a href="contacts.htm" ...>
<img ...>
</a>

If JS is disabled, the user ends up on a page with all relevant contact details, that perhaps has all email addresses as images.

Kelt

10:08 pm on Sep 2, 2004 (gmt 0)

10+ Year Member



OK, lots to think about here then...
Thanks to SS & Bernard for the suggestions and replies - I grovel before the js masters. I had no idea calling a script from within a rollover would be quite this complex.
The 'contact.gif' actually has the email address as a graphic, but obviously if the whole package including the graphics resides in another location, that's not much use to js-challenged surfers. I may end up using the HTML variant.
I'll post back to let you know what happens. Thanks once more.

Damn the spammers...

Bernard Marx

8:45 am on Sep 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had no idea calling a script from within a rollover would be quite this complex.

It's not at all complex doing that (see the second method). What can become difficult is using document.write to write a single attribute/value (our href) into a link tag with nested image - we end up having to write the whole lot. In this respect, document.write is harder to use than the equivalent functions in ASP, PHP (which would have no place here, of course).