最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Save or display long string in JavaScript - Stack Overflow

programmeradmin3浏览0评论

I'm using Mozilla Firefox's console to run some JavaScript on blog to make an organized dump of the posts on it, and store it as a string variable. The string contains about 5000 messages, so it is quite long. I want to somehow save this string on my computer; this part may be done outside using methods outside of JavaScript.

The following options come to mind:

  1. Save the string as a txt file.
  2. Save the contents of the string to the clipboard, paste it in Notepad, then save it.
  3. Use the console's output and copy it from there.
  4. Show an alert, then copy it from there.
  5. Create a new HTML page with the string as the body and show it in a new window, then copy it from there.

However, I don't know how to do 1 and 2 in JavaScript, the string is too long for options 3 and 4 (3 complains about the string being too large when I expand it, 4 gets truncated), and I don't know how to do 5.

Any suggestions? Thank you in advance.

I'm using Mozilla Firefox's console to run some JavaScript on blog to make an organized dump of the posts on it, and store it as a string variable. The string contains about 5000 messages, so it is quite long. I want to somehow save this string on my computer; this part may be done outside using methods outside of JavaScript.

The following options come to mind:

  1. Save the string as a txt file.
  2. Save the contents of the string to the clipboard, paste it in Notepad, then save it.
  3. Use the console's output and copy it from there.
  4. Show an alert, then copy it from there.
  5. Create a new HTML page with the string as the body and show it in a new window, then copy it from there.

However, I don't know how to do 1 and 2 in JavaScript, the string is too long for options 3 and 4 (3 complains about the string being too large when I expand it, 4 gets truncated), and I don't know how to do 5.

Any suggestions? Thank you in advance.

Share Improve this question edited May 4, 2017 at 7:01 Just a student 11k2 gold badges44 silver badges71 bronze badges asked Apr 4, 2014 at 20:11 RPFeltzRPFeltz 1,0752 gold badges12 silver badges21 bronze badges 3
  • Why don't you keep it as an Array or Object with different posts? – Ari Commented Apr 4, 2014 at 20:16
  • @Ari Is it possible to save an Array or Object on my computer then? – RPFeltz Commented Apr 4, 2014 at 20:18
  • It seems easy to console.log large arrays and objects, at least in Chrome. – Ari Commented Apr 4, 2014 at 20:20
Add a comment  | 

3 Answers 3

Reset to default 8

I don't know about Firefox's console, but Chrome's console exposes a copy() method that will put strings of any size on your clipboard

What you can do is the use the new HTML5 "download" attribute of an a tag. If you set the attribute to a file name, when clicked, instead of going to the file, it will download it with the file name. How does this help? Well, you can also use the 'data' scheme. If you have this:

<a href="data:text/plain,This is an example message." download="example.txt">click to download</a>

It will cause the file to download. If you use JavaScript to create the a tag, hide it, set the href to "data:text/plain,YourString", and download to "blogDump.txt", then use the click method, it will cause it to download.

EDIT: Example!

var link = document.createElement('a');
link.setAttribute('href', 'data:text/plain,Example');
link.setAttribute('download', 'example.txt');
link.click();

EDIT 2: FireFox doesn't like links that aren't in the DOM being clicked. Second example:

var link = document.createElement('a');
link.setAttribute('href', 'data:text/plain,Example');
link.setAttribute('download', 'example.txt');
document.getElementsByTagName("body")[0].appendChild(link).click();
  1. You cannot save a TXT file with pure Javascript.
  2. You cannot save string to clipboard with pure Javascript (IE only, or using a Flash hack)
  3. You've already said it's too large
  4. Idem 3
  5. It's easy like a cake:

    var newWindow = window.open("");
    var body = newWindow.document.body;
    var text = "innerText" in body ? "innerText" : "textContent";
    body[text] = "YOUR STRING GOES HERE";
    

FIDDLE EXAMPLE

发布评论

评论列表(0)

  1. 暂无评论