HTML JavaScript Marketing

Collecting all the email addresses or links on a webpage with JavaScript

Have you ever wanted, or needed, to collect all of the email addresses or links on a website?

Have you ever tried to do this manually?

I have. It’s incredibly tedious.

There’s nothing quite as dehumanizing as robotically copying and pasting hundreds of email addresses or links from a webpage to an excel sheet over and over again.

So let’s make sure you never have to do that again.

In this guide, I’ll show you how to use JavaScript to collect all of the email addresses or links on a webpage.

It’s actually pretty easy. So let’s get started.

Thinking before writing your code

Before we begin writing our program, it’s always useful to think about what we’re trying to accomplish, this helps us write a more coherent program.

Here’s what we know we’ll need to do for this program:

  1. Search through the content of the webpage for email/links.
  2. Collect the email/links.

Seems pretty straightforward right? It is!

Now let’s break down each of the steps above into actual statements that we’ll need to write to complete the task of collecting emails from a website.

Here’s what we need to do:

  1. Create an empty array to populate with email/links.
  2. Specify where in the DOM we want JavaScript to search.
  3. Convert the content of the DOM to a string.
  4. Use the .match method to specify what we’re searching for.
  5. Add the matched items to our array.

Also pretty straightforward, so let’s start writing some code.

I created a test page for you to practice with. Please open this page in a new window beside this one, activate your developer console, and follow the instructions below.

Collecting all the email addresses on a webpage

Once you’ve opened up this page and turned on your JavaScript developer console, you’ll need to create an empty array to store the email addresses.

Please type this into your JavaScript developer console:

var listOfEmails = [];

Great, now let’s determine where we want JavaScript to look for these email addresses.

In the test page I created for you, the email addresses are within the opening <body> and closing </body> HTML tags of the page.

Let’s write a variable and assign the content within these tags to it:

var contentToSearch = document.body.innerHTML;

Now let’s verify the content assigned to our variable is correct by typing the following in our JavaScript developer console:


Did you see the HTML content of the page appear in your developer console? Excellent.

Now we need to convert the content from HTML to text so we can search it. To do this we’ll use the .toString method and apply it to our contentToSearch variable, which will convert all the HTML to text:

var contentAsText = contentToSearch.toString();

All of the content within the opening <body> and closing </body> HTML tags have just been converted to text and assigned to the variable contentAsText.

Let’s now search through it for the email addresses.

To do this we’ll use the .match method on the variable contentAsText in conjunction with a regular expression which matches some standard email address patterns:

listOfEmails = contentAsText.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);

Now, to access the list of emails you just need one final step, type the following in your browser console:


You should see your list of emails! Well done!

Here’s the entire program we just wrote.

Final program for collecting all the email addresses on a webpage

var listOfEmails = [];
var contentToSearch = document.body.innerHTML;
var contentAsText = contentToSearch.toString();
listOfEmails = contentAsText.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);

Collecting all the links on a webpage

This will be pretty similar to the program we wrote for collecting all the email addresses.

Please open this page in a new window beside this one, activate your developer console, and start by creating an array to store our links:

var listOfLinks = [];

Now let’s collect all the links on the page.

Lucky for us, there’s a default JavaScript method called .links which can collect all the links on a page for us, without the need for writing a custom function.

Let’s write a variable to use the .links method:

var collectLinks = document.links;

Now we’ll need to loop through the links one by one and add them to our array.

Let’s do this with a JavaScript loop which utilizes the .push method:

for(var i=0; i<collectLinks.length; i++) {

Did you see the number 12 in your developer console?

If you did, congratulations, you’ve just collected all of the links on the page.

Now let’s take a look at the content of our array by typing the following:


Notice it collected all the email addresses and links?

That’s perfectly normal since both email addresses and links use the HTML tag a href.

Well done!

Here’s the entire program we just wrote.

Final program for collecting all the links on a webpage

var listOfLinks = [];
var collectLinks = document.links;
for(var i=0; i<collectLinks.length; i++) {

Why this works

You might be wondering how this is possible, considering you don’t have the ability to edit the JavaScript files on a website you are visiting.

Well, you don’t actually need to edit a website’s JavaScript files directly as you can run the JavaScript program in your web browser’s developer console. This is actually one of JavaScript’s best features!

So, with only our web browser and a little bit of JavaScript know how we were able to collect all the email addresses and links on a webpage.

Pretty handy right?

Next, let’s learn how to add some intelligence to our programs with conditional statements which let our program make decisions on their own.

Branding Marketing Target Audience

Reaching your target audience with content marketing

This is the third installment of my building a brand from scratch series. It focuses on reaching your brand’s target audience. This series is most effective when read in sequence. Here’s the first part of the series if you’d like to start from there.

Any seasoned brand-builder will tell you that knowing your target audience is not enough. You must also reach them. And if you can consistently reach them, then you are well on your way to building a brand.

After I had defined my target audience’s demographics and psychographic characteristics with help from the Facebook Audience Insights tool, I started to think about how to put this information about my target audience to work.

I needed a way to reach my target audience that was affordable, sustainable and expandable.

The most obvious application was content marketing.

Content Marketing? What’s Content Marketing?

“Content marketing is a strategic marketing approach focused on creating and distributing valuable, relevant, and consistent content to attract and retain a clearly defined audience — and, ultimately, to drive profitable customer action.”

Content Marketing Institute

In other words, if you are creating blogs, photos, videos, podcasts or social media posts with the goal of getting the attention of your target audience you are content marketing.

How does the content you create help get attention for your brand?

Think of a social media feed. Think of how content appears in the feed. Usually, it’s something like this:

Look close, what do you notice?

Notice what’s attached to the content in the feed: a profile. Often, a brand profile.

So if people are paying attention to your content, they are paying attention to your brand. And if you are creating content to get the attention of your target audience, then you are content marketing.

How you can start content marketing

As mentioned there are many ways to do content marketing: blogs, photos, video, podcasts, social media posts, etc., and each of these methods has its own pros and cons. My approach uses images and is built on three proven business principles: easy, affordable, expandable.

Images are the time-tested favourite as they meet all three of the above criteria and the process I use is the result of hundreds of hours of building brands from scratch and developing content marketing processes. If you follow my approach to content marketing I guarantee you will save time, money and a good deal of sanity.

But how do I make content that my audience will think is awesome?

The demographic and psychographic information Facebook Audience Insights tool revealed about your target audience in part two will help you make content that your audience will think is awesome.

Here’s what this looks like in practice. We’ll use my t-shirt brand Danskii as an example.

For Danskii my target audience is composed of 25-34-year-old females with a college education and a preference for travel and photography.

So… how do I use this information to do that?


Travel and photography…

Travel and photography?

Travel and photography!


There’s how I make content to get my audience’s attention. I’ll need to create images that focus on travel.

Ok, so admittedly the figuring out what will appeal to your target audience step is always pretty easy.

Creating photos that appeal to your target audience is where the real work comes in.

Lucky for you I’m going to outline exactly how to do this.

Facebook Marketing Target Audience

Identifying your target audience

This is the second installment of my building a brand from scratch series. It focuses on reaching your brand’s target audience. This series is most effective when read in sequence. Here’s the first part of the series, starting a brand from scratch, if you’d like to start from there.

Whether you are selling high-end shoes, making dog yoga videos or writing about how you built a brand from scratch, knowing your target audience is absolutely essential.

I was feeling pretty good after completing my website and setting up the Instagram account for my t-shirt company.

There’s nothing as self-gratifying as the feeling of pulling up your new website and showing it off to your friends.

There’s also nothing quite like the feeling of knowing no one else is seeing it. 0-traffic-itis is painful. And since website visitors don’t manifest from the ether, I needed to start thinking about building traffic.

My target audiences

When I began my dropshipping business in December of 2017 I relied primarily on advertising to drive traffic. This worked just fine for sending people to my website and provided an added bonus of this really cool upward trending line.

Nearly 50! Wow!

But that upward trending line was the only thing the advertising did.

One of the major reasons for my initial dropshipping business failure was that I had not identified a target audience early enough.

This mistake caused my content and advertisements to be far too generic when they should have been tailored to a specific group of people.

I learned, through trial and error and many hours of research that the more tailored to an audience your content and advertising is, the more effective it will be.

The realization of my failure to effectively define a target audience crept up on me slowly.

While I had people visiting my site, the majority were not doing anything of value. No sharing, no shopping, nothing. Worse, I was burning through money at an alarming rate.

As you can imagine, my untargeted approach got very expensive very fast. And the results were minimal.

“One of the biggest mistakes that budding personal branders make is trying to appeal to everyone. Think about the game of darts: You have to aim in order to hit the board. (If you let your darts go without aiming them, you probably won’t be very popular.) If you hit the board, you score. And if your aim is very good and you hit the bull’s eye, even better!”


However, advertising on Facebook introduced me to a unique and incredibly powerful tool: Facebook Audience Insights

Audience Insights was created by Facebook to allow advertisers to tap into the nearly infinite supply of demographic data that Facebook collects from every user: age, gender, occupation, likes, income, you name it.

Look at all that sweet glorious demographic data

“The more customer insights you have, the better you’re equipped to deliver meaningful messages to people. That’s the thinking behind Facebook Audience Insights, a new tool designed to help marketers learn more about their target audiences, including aggregate information about geography, demographics, purchase”

Facebook Audience Insights

Facebook Audience Insights

The Audience Insights tool was exactly what I needed to identify my target audience for my fledgling t-shirt company.

Here’s how I used the tool to do just that.

  1. Login to Facebook
  2. Navigate to the Audience Insights tools
  3. Think of a brand which is similar to your own (the brand you select must also have a Facebook page for this to work)
  4. Under the interests text input, type the brand name
  5. Make note of the details which have the highest percentages for each category:
    • Gender
    • Age
    • Relationship Status
    • Education Level
    • Page Likes
    • Location
    • Device Users
Here’s where to find Gender, Age, Relationship Status and Education Level. You’ll need to explore the tool a bit to find Page Likes, Location and Device Users.

After spending some time with the Audience Insights tool, I eventually landed on a brand that was similar to my own.

After reviewing the brand, via the process listed above, I was able to quickly access an extremely detailed set of demographic data about my target audience. This was pure gold as it now gave me the information I needed to start tailoring my content.

“The more customer insights you have, the better you’re equipped to deliver meaningful messages to people…”

Facebook Audience Insights

So there it was, I had discovered my target audience and completed one of the most important steps to building a brand.

Now it was time to figure out how to reach them.