Created
June 7, 2018 18:14
-
-
Save rsiddle/8f8379f39a0ee21c5229007827add6c5 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Merj - JavaScript onClick Link Example</title> | |
<style> | |
.js-link { | |
color: blue; | |
text-decoration: underline; | |
cursor: pointer; | |
} | |
</style> | |
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> | |
</head> | |
<body> | |
<p>Example 1: This is a JavaScript link to <a data-href="https://merj.com" target="_blank" rel="noopener noreferrer" class="js-link" id="merj-1">Merj.com</a>.</p> | |
<p>Example 2: This is a standard link to <a href="https://merj.com" target="_blank" rel="noopener noreferrer" id="merj-2">Merj.com</a>.</p> | |
<script> | |
$(document).ready(function() { | |
var jsUrl = function(element) { | |
$(element).on('click', function() { | |
var _$this = $(this); | |
var _target = _$this.attr('target'); | |
var _url = _$this.attr('data-href'); | |
if (_target === '_blank') { | |
var w = window.open(); | |
w.opener = null; // Stop Reverse Tab Nabbing. | |
w.location = _url; | |
} else { | |
location.href = _url; | |
} | |
}); | |
}; | |
// Bind elements to function. | |
jsUrl('.js-link'); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment