0

I can't use JSON.Stringify because the document object goes many many levels deep. I tried the function over at: http://www.davidpirek.com/blog/object-to-string-how-to-deserialize-json but I get an error _o.hasOwnProperty is not a function. Anyone have any advice or tips?

Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • 2
    Why on earth do you want to serialize the document object? – lonesomeday Nov 12 '11 at 18:42
  • 4
    What are you really trying to accomplish? – jfriend00 Nov 12 '11 at 18:42
  • Scraping the HTML (and all associated document information) – Shamoon Nov 12 '11 at 18:43
  • If you really want to pull down an entire page, with all scripts, stylesheets, images etc. you likely do not want to do it with JavaScript but instead using PHP/JSP/ASP... or good ole fashion wGet. – scunliffe Nov 12 '11 at 18:54
  • Except I want to do it while the user is on a page.. – Shamoon Nov 12 '11 at 18:55
  • Please take a moment to explain your "end goal" there is very likely a much better way to accomplish whatever it is that you are *really* after. – scunliffe Nov 12 '11 at 18:56
  • What I want is for a user on page to send me back the full HTML of the page that they are on – Shamoon Nov 12 '11 at 18:59
  • This doesn't seem to make much sense... if you have the source of the page there's no point in them sending it to you... it will be huge, and you'll still have to parse it. Any data you care about should be in the form... and if you want additional info, use JavaScript to put that info into a hidden form element (e.g. the mouse position, screen size, time on page, or whatever else you want). – scunliffe Nov 12 '11 at 19:01
  • why don't u use document.body.innerHTML ? – Nakul Nov 12 '11 at 19:02
  • document.body doesn't seem to exist when I try to call it – Shamoon Nov 12 '11 at 19:09
  • I meant document.body.innerHTML – Nakul Nov 12 '11 at 19:20

2 Answers2

1

You can not serialize 'document' to JSON string using JSON.stringify, since it contains circular references. One way to deal with circular references is to implement your own stringify method, which keeps tracks of the circular references and excludes those. (see this answer: How to solve circular reference in json serializer caused by hibernate bidirectional mapping?)

As already said in the comments, do you really need to serialize the document object?

I've been working with a similar kind of project (time to advertise: http://hannotaatio.futurice.com) and we do not serialize document object. Instead, relevant information from document object is stored using JavaScript. The code is on Github so feel free to check it out.

Community
  • 1
  • 1
rap1ds
  • 629
  • 1
  • 5
  • 10
0

Apparently one way to go is simply to use jQuery. I don't love it, but it'll do for now.

Shamoon
  • 41,293
  • 91
  • 306
  • 570