window.location.href不适用于mac safari <a>?

时间:2016-08-22 10:37:21

标签: javascript jquery

have made a anchro tag with 2 urls. One is href and other is data-url. When i click on button it should open a page in new tab which is correct. But additionally what i want is to reload the current page with the data-url link that i set.

So in the example i set href to google.com and it opens in new tab. Its correct but when we click the button i additionally the the current page to reload to data-url that is youtube.com.

HTML

<a href="https://www.google.com/" target="_blank" data-url="https://www.facebook.com/" id="agree">Click me</a>

JS

$('#agree').on('click', function() {
    var url = $(this).data('url');
    window.location.href = url;
});

Link To Codepen

1 个答案:

答案 0 :(得分:0)

请试试这个:D

jQuery(function(){
	jQuery('#agree').on('click', function(e) {
		e.preventDefault();
		var redirectUrl = jQuery(this).attr('href');
		var url = jQuery(this).data('url');
		window.location.href= redirectUrl;
		window.open( url, '_blank');
	});
});
#agree {
  background: red;
  padding: 10px;
  color: #fff;
  display: inline-block;
  text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="https://www.google.com/" data-url="https://www.facebook.com/" id="agree">Click me</a>