0

I am trying to show a download pop up to the user which is an ajax call giving text/csv formatted response.

The issue: it's working well in Firefox but not in Chrome, where it's opening a new tab. This is definitely unintended, and I would prefer it didn't happen. Any ideas on what I could change...?

function x() {

  var obj = jQuery("input:checked").map(function() 
  { 
    return jQuery(this).parents('tr').attr('id'); 
  });

  var result = null;
  var arr = jQuery.makeArray(obj);
  var data = arr.join(',');

  $.ajax({
    url : '<%= url_for :controller => "liges", :action => "export_to_csv" %>',
    type : 'POST',
    data : {data:data},
    dataType : 'string',
    async: false,
    success : function(response) { 
      window.open('data:text/csv;charset=utf-8,'+escape(response)); 
    }    
  }); 
};
daOnlyBG
  • 595
  • 4
  • 20
  • 49
JstRoRR
  • 3,693
  • 2
  • 19
  • 20

2 Answers2

1

Try this:

window.open("","","width=400,height=150");

By the way chrome will block popup windows on page, until you give him access.

kirugan
  • 2,514
  • 2
  • 22
  • 41
0

You need to pass _blank as another parameter:

From MDN:

To open a new window on every call of window.open(), use the special value _blank for strWindowName.

Example:

window.open('data:text/csv;charset=utf-8,'+escape(response), '_blank'); 
epoch
  • 16,396
  • 4
  • 43
  • 71
  • _blank did not help. It showing me the same stuff!! a new tab with all the csv content as html. – JstRoRR Oct 10 '12 at 10:36
  • so you don't have any pop-up blockers running? – epoch Oct 10 '12 at 10:49
  • how about thinking of a different way to display the csv, most users will have pup-up blockers in any case, try sending the content to a hidden `iframe` to start the download, or display it in a `div` – epoch Oct 10 '12 at 11:05
  • http://stackoverflow.com/questions/365777/starting-file-download-with-javascript here is the iframe example i found..but will this way work with rails..??? in that case, in the path i need to provide the controller and action?? – JstRoRR Oct 10 '12 at 11:31