Forum Moderators: open
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.
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
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.
<!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>
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>