16

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?

1

4 Answers 4

12

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/

Sign up to request clarification or add additional context in comments.

5 Comments

ok, my problem is: i'm in a browser, i see the object with Firebug, but i can't save it. I can put javascript into the original page with GreaseMonkey, but the alert window is too small for JSONed object text and i can't write file from JavaScript.
Well, you're limited no 0 operations with Javascript, so your best bet is this one. I can't understand why this won't work for you. You can copy/paste the JSONed object from Firebug's console and then use jsonviewer.stack.hu Or maybe I'm getting it wrong? :s
Yes, excuse me, I ment to say the "stringified" object.
hi i have no experiences with this tools :( i wrote to firebug console this: JSON.stringify($I) but the result is "{"$n":"$I"}" and i see this in DOM tab:link i would like to save this complex object with the name of methodes and the values of properties :) i think i do something wrong, You said it is a very simple thing, but not for me :(
Uncaught TypeError: Converting circular structure to JSON
10

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.

Comments

3

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 (){}

Comments

0

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.

1 Comment

i don't want use JavaScript to save, if exists any browser plugin which can SHOW (like Firebug) AND SAVE (Firebug can't) the object, i'm happy with it :) and currently i haven't any servside engine :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.