javscript and XHTML

  Awestruck 19:20 22 Feb 09
Locked

I have web page containing an email scrambler in the body of the page as follows:-
script type="text/javascript">
!--
function escramble(){
var a,b,c,d,e,f,g,h,i
a=' a href=\"mai'
b='fbloggs'
c='\">'
a+='lto:'
b+='@'
e=' /a>'
f='Click here!'
b+='isp.co.uk'
g=' img src=\"'
h='images/email.gif'
i='\" alt="Email me." border="0">'
if (f) d=f
else if (h) d=g+h+i
else d=b
document.write(a+b+c+d+e)
}
escramble()
//-->
/script>
The page validates on W3C as an HTML 4.01 transitional page. However, when I put the same script on an XHTML version of the page I get over 40 errors when validating as XHTML transitional. All of the errors concern the javascript item.

W3C mentions that problems may occur with javascript but the advice given is very sketchy. I would be grateful for advice on how to make the javascript validate (lots of escape characters I suspect).
Awestruck

  Kemistri 00:39 23 Feb 09

Move you JS out to a separate file, which is where it should be anyway.

  Awestruck 11:41 23 Feb 09

Thanks for the tip Kemistri. I started off using an external .js file but I could not persuade it to show the email.gif. As a result I gave up trying and put the whole script into the body of the page, then it did show the image.

Perhaps you could point out where I am going wrong. In the position on the page where I wanted the email address picture to show I put the following:-
<script type="text/javascript" src="escramble.js">
</script>
The external .js file was as follows:-
function escramble(){
var a,b,c,d,e,f,g,h,i
a=' a href=\"mai'
b='fbloggs'
c='\">'
a+='lto:'
b+='@'
e=' /a>'
f='Click here!'
b+='isp.co.uk'
g=' img src=\"'
h='images/email.gif'
i='\" alt="Email me." border="0">'
if (f) d=f
else if (h) d=g+h+i
else d=b
document.write(a+b+c+d+e)
}

Unfortunately the script seemed unable to find the image and display it.
Awestruck

  Kemistri 15:28 23 Feb 09

First check your filepath, then run it through Firebug. Are you using a library?

  Awestruck 16:13 23 Feb 09

I am not using a library. I will download Firebug and check the script.
Regards
Awestruck

  Awestruck 18:19 24 Feb 09

Hello Kemistri
Firebug found nothing wrong. The file path is correct.
I tried putting the email.gif file in the main folder instead of in the images folder and changed the path accordingly in the external js file.
It still did not work.
The email.gif file fails to appear only when I use an external js file.
Using the javascript within either an HTML or an XHTML page works fine. However, then it will not validate for XHTML.
I found a work around on the internet which I will try out, but I am not fond of work arounds, I would rather solve the problem by correction.
The work around goes like this:-
<script type="text/javascript">
//<![CDATA[

full script goes here

//]]>
</script>

Regards Awestruck

This thread is now locked and can not be replied to.

Surface Pro (2017) vs Surface Pro 4

20 groundbreaking 3D animation techniques

How to mine Bitcoin on Mac