Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validator @ W3 not liking mailto: statement 1

Status
Not open for further replies.

GPDynamics

IS-IT--Management
Dec 8, 2009
3
0
0
US
The error message I get is:
document type does not allow element "a" here

The validator indicates the issue is with the > just before My Name and Contact

lines of code with issue:
document.write('<a href="mailto:' + email + '">My Name</a>');

document.write('<a href="mailto:' + email + '">Contact</a>');

Full Code in Header:
<script type="text/javascript">
var name = "Michael";
var char1 = "@";
var domain = "Domain";
var char2 = ".";
var tld = "com";
var email = name + char1 + domain + char2 + tld

function HideEmail()
{
document.write('<a href="mailto:' + email + '">My Name</a>');
}

function HideContactEmail()
{
document.write('<a href="mailto:' + email + '">Contact</a>');
}
</script>

Any help would be greatly appreciated!
Thanks!
Michael

 
I've had to do this in the past to fool the validator:
Code:
<!-- Begin
var mname;
var mdom;
var suf;
var dispname;

function writeemail(mname, mdom, suf, dispname){
document.write('<a href="mai' + 'lto:' + mname + '@' + mdom + '.' + suf + '" title="Email ' + dispname + '">' + dispname + '</' + 'a>');
}
//  End -->

then in the html
Code:
<script type="text/javascript">
<!-- Begin
 writeemail("Michael", "domain", "com", "My Name");
//  End -->
</script>

which produces the output desired.

Greg
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
 
This a perfect example of what the CDATA directive is for:

Code:
<script type="text/javascript">
//<![CDATA[

	document.write('<a href="mailto:' + email + '">My Name</a>');

//]]>
</script>

Why hack the code and make it far less readable when there's just no need to?

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
...or just put the javascript into a seperate file, leaving the validator with only HTML to validate:
Code:
<script type="text/javascript" src="/scripts/email.js"></script>
This has the benefit of giving you just one file to change if you change your contact details.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
BillyRayPreachersSon said:
Why hack the code and make it far less readable when there's just no need to?

Excellent point. CDATA is a much cleaner solution.

[rationalization]The original code (from very long ago) was really written to obfuscate the emails for spammers that couldn't read JavaScript (more so than fooling the validator). [/rationalization] [blush]



Greg
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
 
Thanks for the feedback. However, I can't seem to get the page to render the results from the .js file.

I get no errors....but I also get nothing to appear.

Header:
<script type="text/javascript" src="scripts/emailnopass.js"></script>

Body:
<script type="text/javascript" src="scripts/emailnopass.js">
HideEmail();
</script>

.js:
function HideEmail() {
var fname = "Michael";
var char1 = "@";
var domain = "Domain";
var char2 = ".";
var suf = "com";
var email = fname + char1 + domain + char2 + suf;

document.write('<a href="mailto:' + email + '" title="e-mail ' + fname + '">' + 'My Name</a>');
}

Once again, all feedback is appreciated.
Thanks!

Michael

 
 http://www.recursivecreativity.com/Pages/Page-Misc.html
If you use the src attribute, you can't put a function call inside the JS tags. Gotta be something like the following in the body:
Code:
<script type="text/javascript">
     HideEmail();
</script>

Lee
 
I tried that originally but keep getting:
Error: Object expected.

I even tried:
.js:

var fname = "Michael";
var char1 = "@";
var domain = "Domain";
var char2 = ".";
var suf = "com";
var email = fname + char1 + domain + char2 + suf;

function HideEmail()
{
document.write('<a href="mailto:' + email + '" title="e-mail ' + fname + '">' + 'My Name</a>');
}

Got to be something that a Newby like me (oun intended) is overkooking!

Thanks!
Michael

 
 http://www.recursivecreativity.com/Pages/Page-Misc.html
You need BOTH script tags, the one to load the external JS file and the one to call the function, the way you're trying to do it now.

Or you can put the HideMail() function call in the external JS file and then just load it (your first example) where you want it to display.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top