0

I have a script I am using that will change a link from a regular link to a mailto link. The idea is most bad robots will not parse javascript thus is a bit better than putting the email address as is or spacing and spelling the @ symbol.

The HTML should render as:

Contact our Sales Manager by email.

The word email would be a link to the email. However with the JS code it renders as so:

Contact our Sales Manager by [email protected].

I would like the first sentence to be how the page renders.

Here is the code:

HTML:

<p>Contact our Sales Manager by
   <a class="email" title="email/email.com" href=" ">email</a>.
</p> 

JS:

$(function() {

  $('a.email').each(function(){

    var e = this.title.replace('/','@');

    $(this).text().replace('/','@');

    this.href=" ";

    this.href = 'mailto:' + e; $(this).text(e);

   });
});

How would I modify the script to leave the word email in there but create the link correctly?

3
  • I suggest you to read more about javascript and string manipulation Commented Dec 5, 2012 at 4:29
  • 1
    @pylover - I am learning JS and taking it step by step. Commented Dec 5, 2012 at 4:31
  • see my answer and jsfiddle example Commented Dec 5, 2012 at 4:39

4 Answers 4

1

Here is a working example: http://jsfiddle.net/smLQ7/

To fix this, simply remove one part of the JavaScript so it is like this:

$(document).ready(function() {
  $('a.email').each(function(){
    var e = this.title.replace('/','@');
    $(this).text().replace('/','@');
    this.href=" ";
    this.href = 'mailto:' + e;
   });
});​

We have removed this code:

$(this).text(e);

It was responsible for replacing email with [email protected] in the page contents.

Sign up to request clarification or add additional context in comments.

Comments

1

If you're going to do it via JS then why bother putting the address in the tag at all?

Why not just try something like this on (document).ready()

var theaddress ="mailto:[email protected]"

$('a.email').attr('href',theaddress);

EDIT:

Working here: http://jsfiddle.net/sMnrR/

1 Comment

I have thought of that but I use templates and I can stick the code in the template and then just add the class email and title to the specific email addresses. It seems convoluted I know but in my case it works better. Though I may use your example in other places. Thanks!
1

Replace

$(this).text(e)

by

$(this).text('email');

text('some string') replaces the content of the element by its argument. So in your case, the link's content was being replaced by the email address.

Comments

0

If understand your problem, Here is solution:

$(function() {
  $('a.email').each(function(){

    var e = this.title.replace('/','@');
    $(this)
        .attr('href','mailto:' + e)
        .text(e.substr(0,e.indexOf("@")));
   });
});​

Take a look at jsfiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.