I need to save a complex javascript object to a file for later investigation. It is a very big object, more then 50 methods and propeties.
I can see the object and its methods and properties (and its values) in Firefox-Firebug on DOM page, but i can't save it to a file from there.
I want save the object with current values of properties, not the HTML doc. Any format of a file - HTML or JSON or anything else is good for me :)
How can I save the object?
Well... There is something you can do, but I can't say how ugly 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/
You can pass the object from Your page to server-side script using AJAX like this (jQuery):
var ajax_object // Your object
.ajax({
type: "POST",
url: "tofile.php",
data: ajax_object,
});
and then write it down to a HTML file using the server-side script (example is 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 output in somefile.htm would be similar to this:
Some_Object Object
(
[some_element] => 123
[some_array] => Array
(
[element1] => 456
[element2] => 789
[element3] => 012
)
)
If You are wondering how to save Your object to a file using only Javascript, without server-side language, then, I am afraid, it is not possible.
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();
Result of this method is saved txt file with text:
String first = string
Function second = function (){}
It is possible now using 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 it here: https://developer.mozilla.org/en-US/docs/Web/API/Blob
It requires that you have a webpage up with a live DOM, which should work if you are in the middle of debugging javascript on a page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.