Created
August 15, 2014 13:06
-
-
Save natewinck/7b2d1698dd0ad8288bfb to your computer and use it in GitHub Desktop.
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
// Create a link out of any element while maintaining | |
// the ability to open those links in a new window/tab/etc. | |
$("body").on("click", "[data-href]", function(e) { | |
// Create the anchor based on the data-href attribute | |
$anchor = $("<a>").attr("href", $(this).attr("data-href")); | |
$anchor.css("display", "none"); | |
// Check to see if the user is holding a modifier key | |
if (e.ctrlKey || e.shiftKey || e.metaKey) { | |
$anchor.attr("target", "_blank"); | |
} | |
// Add the anchor to the end of the document | |
$("body").append($anchor); | |
// Simulate a click | |
$anchor[0].click(); | |
// Remove the anchor from the document | |
$anchor.remove(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment