Skip to content

Instantly share code, notes, and snippets.

@bmhaskar
Created December 18, 2014 21:06
Show Gist options
  • Save bmhaskar/83aec826a8c0913a5a1d to your computer and use it in GitHub Desktop.
Save bmhaskar/83aec826a8c0913a5a1d to your computer and use it in GitHub Desktop.
This is sample page demonstrating referrer masking with the help of JS without iframe.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Mask A referrer in get request without iframe</title>
</head>
<body>
<a href="http://google.com" class="masked" >Clicking to google</a> ||
<a href="http://yahoo.com" class="masked-differently" id="yahooLink" target="_blank">Clicking to yahoo</a>
<script type="text/javascript">
(
function(root,factory){
root.maskReferer = factory();
}(this,function() {
var that = this;
var handleClick = function(event) {
event.preventDefault();
var tag = event.srcElement || event.target;
var href = tag.getAttribute("href");
var location = getLocation(href);
if(tag.getAttribute("target") == "_blank") {
var newWindow = that.open("about:blank","_blank");
newWindow.location = location;
} else {
that.location = location;
}
};
var getLocation = function(href) {
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
// At least Safari 3+: "[object HTMLElementConstructor]"
var isChrome = !!window.chrome && !isOpera; // Chrome 1+
if(isChrome || isSafari) {
return "data:text/html,"+"<scr"+"ipt>location='" + href + "'</scr"+"ipt>";
}
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
if(isFirefox || isOpera) {
return "data:text/html,"+"<"+"html><meta http-equiv=\"refresh\" content=\"0; url='"+ href +"'\"><\/html>";
}
return href;
};
var maskReferer = function(elements) {
if(elements.ELEMENT_NODE == 1) {
elements.addEventListener("click", handleClick);
} else {
Array.prototype.forEach.call(elements, function (el) {
// Do stuff with the element
el.addEventListener("click", handleClick);
});
}
};
return maskReferer;
})
)
</script>
<script type="text/javascript" >
maskReferer(document.getElementsByClassName("masked"));
maskReferer(document.getElementById("yahooLink"));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment