Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Transfer to a separate js file

I am a noob in javascript, I need to hide a piece of code that I use in html in a separate js file and use it in several pages of the site, how can I do this?

This is the code that protects mail from spambots:

<a href="mailto:testx@testsitex.com" onmouseover="this.href=this.href.replace(/x/g,'');">test@testsite.com</a>

How can I transfer this code onmouseover="this.href=this.href.replace(/x/g,'');" to a separate js file and then use it here?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you can add a class to your email links

<a href="mailto:testx@testsitex.com" class="protected">test@testsite.com</a>

create a js file mouseover.js and add this code to it:

    document.addEventListener("mouseover", (e)=>{
      if(e.target.className === 'protected') e.target.href=e.target.href.replace(/x/g,'');
    });

and add this file to every page that contains email links (can be more)

<script type="text/javascript" src="mouseover.js"></script>

document.addEventListener("mouseover", (e)=>{
  if(e.target.className === 'protected') {
  e.target.href=e.target.href.replace(/x/g,'');
  console.log(e.target.href);
  }
});
<a href="mailto:testx1@testsitex.com" class="protected">test1@testsite.com</a>
<a href="mailto:testx2@testsitex.com" class="protected">test2@testsite.com</a>
<a href="mailto:testx3@testsitex.com" class="protected">test3@testsite.com</a>
<a href="mailto:testx4@testsitex.com" class="protected">test4@testsite.com</a>
<a href="mailto:testx5@testsitex.com" class="protected">test5@testsite.com</a>

about 4 years ago · Juan Pablo Isaza Report

0

You can use for example unicode and unescape function:

var hidden = unescape("\u0048\u0069\u0064\u0064\u0065\u006e \u006d\u0065\u0073\u0073\u0061\u0067\u0065\u0021");
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!