1

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.

Community
  • 1
  • 1
Richard
  • 62,943
  • 126
  • 334
  • 542

1 Answers1

3

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);
});
epascarello
  • 204,599
  • 20
  • 195
  • 236