Richard
Richard

Reputation: 65510

Use window.open without resetting hash?

I have a window with a hash parameter set.

If I then do window.open (I want to allow the user to download some data as CSV), it resets the hash in the current window:

window.location.hash = 'helloworld';
var csvContent = "data:text/csv;charset=utf-8,id,name\n3,james')";
var encodedUri = encodeURI(csvContent);
$('#data-link').on('click', function() {
  window.open(encodedUri);
});

How can I use window.open without resetting the hash?

It's a bit hard to demo this in jsfiddle, but hopefully the above code shows the problem.

Upvotes: 1

Views: 474

Answers (1)

epascarello
epascarello

Reputation: 207501

If the url is changing, than you are using a link or a button which is navigating the page. Cancel the click action so the page does not redirect.

$('#data-link').on('click', function(evt) {
    evt.preventDefault();
    window.open(encodedUri);
});

Upvotes: 3

Related Questions