How to save a Javascript object to a file?

I need to save a complex javascript object to a file for later research. This is a very large object, more than 50 methods and prophecies.

I see the object and its methods and properties (and its values) in Firefox-Firebug on the DOM page, but I cannot save it to a file from there.

I want to save an object with current property values, and not with an HTML document. Any file format - HTML or JSON or something else is good for me :)

How to save an object?

+8
javascript
source share
4 answers

Well ... Something can be done, but I cannot say how ugly it is. You can do something like

JSON.stringify(my_big_javascript_object) , and then save the resulting JSON (plain text) in a file.

You can look at the values ​​later using some JSON viewer like http://jsonviewer.stack.hu/

+7
source share

You can pass the object from your page to the server script using AJAX, like this (jQuery):

 var ajax_object // Your object .ajax({ type: "POST", url: "tofile.php", data: ajax_object, }); 

and then write it to the HTML file using the server-side version of the script (example using PHP):

 // File: tofile.php $ajax_object // Object, which You have passed using AJAX ob_start(); print_r("<pre>".print_r($ajax_object, true)."</pre>"); $var = ob_get_contents(); ob_end_clean(); $fp = fopen('somefile.htm', 'w'); fputs($fp, $var); fclose($fp); 

The result in somefile.htm will look like this:

 Some_Object Object ( [some_element] => 123 [some_array] => Array ( [element1] => 456 [element2] => 789 [element3] => 012 ) ) 

If you are interested in how to save your object in a file using only Javascript, without a server language, then I am afraid this is not possible.

+1
source share

Now you can use the Blob API:

  function downloadObject(obj, filename){ var blob = new Blob([JSON.stringify(obj, null, 2)], {type: "application/json;charset=utf-8"}).slice(2,-1); var url = URL.createObjectURL(blob); var elem = document.createElement("a"); elem.href = url; elem.download = filename; document.body.appendChild(elem); elem.click(); document.body.removeChild(elem); } 

You can read more about this here: https://developer.mozilla.org/en-US/docs/Web/API/Blob

This requires that you have a live DOM webpage that should work if you are in the middle of debugging javascript on the page.

0
source share

This works well for me.

 //Initialization of object to save var objectToSave={first:'string', second: function(){}}; //Start of saving method var textToSave='';//String to be saved var count=0; for(var i in objectToSave){ //Adding every key name + type + text value to string textToSave+=objectToSave[i].constructor.name+' '+ Object.keys(objectToSave)[count]+' = '+objectToSave[i]+'\n'; count++; } //Saving string to file using html clicking trick var hiddenElement = document.createElement('a'); hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave); hiddenElement.target = '_blank'; hiddenElement.download = 'myFile.txt'; hiddenElement.click(); 

The result of this method is to save the txt file with text:

String first = string

Function second = function () {}

0
source share

All Articles