Forum Moderators: open

Message Too Old, No Replies

javascript ruining my xhtml validation

How to write xhtml valid javascript

         

combitz

12:29 pm on Jun 10, 2007 (gmt 0)

10+ Year Member



I'm trying to use javascript to mask my email from the horrid clutches of evil spam bots. It's always worked well the past using javascript and the document.write method however now I want to ensure that my pages comply to w3c standards.

I get an error with the document.write method


document.write("<a href=" + "mail" + "to:" + username + "@" + hostname + ">" + linktext + "</a>")

error = an attribute value literal can occur in an attribute specification list only after a VI delimiter.
and
document type does not allow element "a" here.

The page displays fine without errors but it won't validate

Any help much appreciated.

Little_G

1:17 pm on Jun 10, 2007 (gmt 0)

10+ Year Member



Hi,

Try using the DOM instead:


ml = document.createElement("a");
ml.setAttribute("href","mail" + "to:" + username + "@" + hostname);
ml.textContent = linktext;
document.body.appendChild(ml);

Andrew

combitz

2:10 pm on Jun 14, 2007 (gmt 0)

10+ Year Member



Well this works fine in firefox but IE bombs out on me. I've been trying to look at the DOM and I'm struggling without any compiler help

i've gone through the usual hello world and created an object called link and added the variables first, second as hello and world to try and put the two character arrays together in the document.write method. All works fine in ff and ie but as soon as I try to add a link nothing validates for me in xhtml.

As soon as I try to use the method .append(<object>) IE dies on me. (Unable to open page?)

Any help please, I just need a finger in the right direction...oh er

cheers

bcolflesh

2:23 pm on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



document.write('<a href="' + 'mail' + 'to:' + username + '@' + hostname + '">' + linktext + '<\/a>')

combitz

2:32 pm on Jun 14, 2007 (gmt 0)

10+ Year Member



as stated earlier. the document.write method won't validate in true xhtml :(

error = document type does not allow element "a" here

It shows and works, but it's important to me that it validates to w3c xhtml standards.

I'll keep reading as I don't understand fully how the append method actually works and how the objects attributes can be used in this way.

thanks for reply though.

bcolflesh

2:35 pm on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Doh - my first reply was valid HTML 4.x

here's a valid XHTML 1.0 Strict:

document.write('&lt;a href="' + 'mail' + 'to:' + username + '@' + hostname + '"&gt;' + linktext + '&lt;/&gt;')

Fotiman

2:36 pm on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The correct DOM method would use createElement to create the link, and createTextNode to create the text of the link:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title>DOM Method for Creating Email Link</title>
<script type="text/javascript">
window.onload = function() {
var mailLink = document.createElement("a");
var username = "Fotiman";
var hostname = "example.com";
var addr = username + "@" + hostname;
mailLink.setAttribute("href", "mailto:" + addr);
mailLink.appendChild(document.createTextNode(addr));
var container = document.getElementById("container");
container.appendChild(mailLink);
};
</script>
</head>
<body>
<div id="container">
</div>
</body>
</html>

Fotiman

2:40 pm on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



document.write in general is sloppy. Avoid it.

Also, keep in mind that this will be inaccessible for anyone who has JavaScript disabled. In that case, you might be able to offer them a much less friendly email alternative:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title></title>
<script type="text/javascript">
window.onload = function() {
var mailLink = document.createElement("a");
var username = "Fotiman";
var hostname = "example.com";
var addr = username + "@" + hostname;
mailLink.setAttribute("href", "mailto:" + addr);
mailLink.appendChild(document.createTextNode(addr));
var container = document.getElementById("container");
container.replaceChild(mailLink, container.childNodes[0]);
};
</script>
</head>
<body>
<div id="container">Fotiman (at) example (dot) com.</div>
</body>
</html>

combitz

3:39 pm on Jun 14, 2007 (gmt 0)

10+ Year Member



Thanks Fotiman both methods work fine. I've still got a lot to learn. Does the learning curve ever mellow out. It's looking rather steep still.

Cheers for the help.

Fotiman

3:47 pm on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The more you use it, the easier it will become. :-)