0

I 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.youtube.com/" id="agree">Click me</a>

JS

 $('#agree').on('click', function(e) {
  var url = $(this).data('url');
  console.log(url);
  // window.open(url);
  location.reload(url);
});

Link To Codepen

2 Answers 2

3

Set the URL to window.location.href.

$('#agree').on('click', function(e) {
    var url = $(this).data('url');
    window.location.href = url;
});
Sign up to request clarification or add additional context in comments.

5 Comments

thanks work . ! A quick question thou . what is difference between window.location.href and location.href
Basically it is the same. But if you somewhere use something like var location = 'foo'; your script will fail. It is saver to access window properties with window. in front. @BipuBajgai
codepen.io/sabipu/pen/ZOkmKw - hey but this doesnot works on mac safari . you know why ?
i cannot add an onsubmit action to my button thou.
0

Change location.reload(url) to location.href = url;. It will work

1 Comment

codepen.io/sabipu/pen/ZOkmKw - het but this deosnot works on mac safari . you know why ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.