Skip to content

Instantly share code, notes, and snippets.

@sehrgut
Last active June 5, 2021 01:23
Show Gist options
  • Save sehrgut/1352623 to your computer and use it in GitHub Desktop.
Save sehrgut/1352623 to your computer and use it in GitHub Desktop.
mask/hijack links with jQuery
/*
jquery.evil.js - mask and hijack links
Copyright (C) 2011 Keith Beckman
Released under the GPLv3
*/
/*
mask(url) replaces the href of each selected link with url, restoring it for
the duration of a click.
hijack(url) replaces the href of each selected link with url for the duration
of a click.
These have slightly-different semantics. mask() allows your code to contain
legitimate links, but hide them only from hover/statusbar inspection, while
hijack() allows you to apply the click-href to links containing already-
inoccuous-looking hrefs. For instance, hijack() could be used to prevent
search engines from seeing the urls users will use; while mask() will do the
opposite. mask() could be used, at least semi-legitimately, to make (i.e. Amazon)
affiliate urls more friendly-looking. It's a stretch, but there it is. I can't
think of a white-hat use for hijack.
*/
(function ($) {
$.fn.mask = function (url) {
var jq = $([0]);
return this.filter('a').each(function (n, el) {
var orig = this.href;
this.href = url;
jq[0] = this;
jq.mousedown(function () { this.href = orig; })
.mouseout(function () { this.href = url; });
});
};
$.fn.hijack = function (url) {
// http://stackoverflow.com/questions/3926119/javascript-how-do-i-make-onclickwindow-location-also-work-when-user-opens-in
var jq = $([0]);
return this.filter('a').each(function (n, el) {
var orig = this.href;
jq[0] = this;
jq.mousedown(function(){ this.href = url; })
.mouseout(function() { this.href = orig; })
});
};
}) (jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment