Skip to content

Instantly share code, notes, and snippets.

@derpyherp
Last active January 7, 2022 14:06
Show Gist options
  • Save derpyherp/87d3ca6533856f96bccd3ed986abc14b to your computer and use it in GitHub Desktop.
Save derpyherp/87d3ca6533856f96bccd3ed986abc14b to your computer and use it in GitHub Desktop.
Show a custom message to social media visitors.
Show a custom message to visitors from social media in JavaScript.
Catches all those pesky subdomains like l.instagram.com and m.facebook.com.
<!DOCTYPE html>
<html lang="en">
<head>
<script src="/referer.js"></script>
</head>
<body>
<div id="greeting"></div>
</body>
</html>
if (document.referrer) {
var source_domain = (document.referrer.split('/')[2]);
var social_media = [
["t.co", "Twitter"],
["reddit.com", "reddit"],
["facebook.com", "Facebook"],
["instagram.com", "Instagram"],
["youtube.com", "YouTube"],
["pinterest.com", "Pinterest"]
];
for (let index = 0; index < social_media.length; ++index) {
const element = social_media[index];
//need to use endsWith or t.co will match reddit.com etc
if (source_domain.endsWith(element[0])) {
pretty_name = element[1];
break;
}
}
}
function show_msg() {
if (pretty_name) {
document.getElementById("greeting").innerHTML = "Welcome, " + pretty_name + " visitor!";
}
}
window.onload = show_msg;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment