Search code examples
javascriptcsvgoogle-chromeurlencodewindow.open

In Google Chrome, window.open fails on csv with # symbol in it


window.open(encodeURI('data:text/csv;charset=utf-8,name,color\njohn,#000000'));

In chrome, the previous line downloads a csv with this content:

name,color
john,

It seems to ignore everything after the # symbol. Do you have an idea why?

ps: On Safari it seems to work well, it opens a news tab with everything in it


Solution

  • Because # denotes the start of a location within the document.

    You'll have to escape it to %23:

    'data:text/csv;charset=utf-8,' + encodeURIComponent("name,color\njohn,#000000")
    

    results in data:text/csv;charset=utf-8,name%2Ccolor%0Ajohn%2C%23000000 which ought to work better.