I want the functionality to open all links in a website to a new background tab by default when a user clicks on the link. I have checked all over but none of them provide a solution that works on all browsers. I have referred the following two links to write my code:
Suggested modifications for ie
I have the following code:
<html>
<head>
<style type="text/css">
.link{ color: #0055ff; cursor: pointer; text-decoration: underline; }
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".link").on('click', function(event){
var $this = $(this);
var a = document.createElement("a");
a.href = $this.href;
if( document.createEvent ) {
var evObj = document.createEvent('MouseEvents');
evObj.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
true, false, false, true, 1, a);
a.dispatchEvent( evObj );
} else if( document.createEventObject ) {
var evObj = document.createEventObject();
evObj.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
true, false, false, true, 1, a);
a.fireEvent( 'onclick', evObj );
}
});
});
</script>
</head>
<body>
<span class="link" data-href="http://www.google.com">Go to Google</span>
</body>
</html>