Last active
August 29, 2015 14:04
-
-
Save thpoiani/0419732cdeb7540247e4 to your computer and use it in GitHub Desktop.
Ways to emulate a click in a link with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Ways to emulate a click in a link with JavaScript</title> | |
<style> | |
[data-href] { cursor: pointer; } | |
</style> | |
</head> | |
<body> | |
<h1 data-href="https://github.com/thpoiani">Click here to see my repositories!</h1> | |
<script> | |
(function (w, d, undefined) { | |
'use strict'; | |
var link = d.querySelector('[data-href]'); | |
link.addEventListener('click', function (event) { | |
var href = this.getAttribute('data-href'); | |
if (event.ctrlKey) { | |
// CTRL + Click | |
w.open(href, '_blank'); | |
} else if (event.shiftKey) { | |
// Shift + Click | |
w.open(href); | |
} else { | |
// Click | |
w.location.href = href; | |
} | |
}); | |
})(window, document); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment