SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    doument.write onclick problems

    Right, here's the deal, i'm new to javascript but have a little knowledge.

    I recently added a document.write script to avoid spam on my email.

    this is the code i used
    Code:
    <script language="JavaScript"><!--
    var name = "NAME";
    var domain = "host.com";
    
    document.write('<a href=\"mailto:' + name + '@' + domain + '\" >');
    document.write(name + '@' + domain + '</a>');
    
    // --></script>
    This above code works, but i want to add to it. I want an alert to appear once clicked. For this, i would use onClick

    Code:
    <script language="JavaScript"><!--
    var name = "NAME";
    var domain = "host.com";
    
    document.write('<a href=\"mailto:' + name + '@' + domain + '\" onClick="window.alert('TEST')">');
    document.write(name + '@' + domain + '</a>');
    
    // --></script>
    as soon as i add the onClick, the code does not work, the text / link doesn't even appear anymore.

    What i would like to know is why this happens and how to successfully add onClick to the above code.

    Thank you for your time.

    Gilligan

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    The single quotes inside the window.alert are causing you trouble, because javascript thinks that you're closing off the document.write string.

    You'll need to escape those quotes, using \' and while we're at it, the double quotes don't need escaping.

    Code JavaScript:
    var name = "NAME";
    var domain = "host.com";
     
    document.write('<a href="mailto:' + name + '@' + domain + '" onClick="window.alert(\'TEST\')">');
    document.write(name + '@' + domain + '</a>');

  3. #3
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that really helped

    Great site as well.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,603
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Most spam spiders are able to read the email address from scripts like that now.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •