0

I'm using JSON.stringify() with a custom replacer to stringify the window object of a given page. On some pages I understandably get the same-origin-policy error that interrupts the execution. What I would like to do is to safely handle the restricted access requests that cause the error by skipping them and continue with the rest of the execution. I could catch the error outside of JSON.stringify() but there is no point as the execution has already stopped...

Is this possible? If not, any other suggestions to get rid of a large object's circular references like window as I'm using JSON.stringify() to get rid of circular references in the first place

Saba Ahang
  • 578
  • 9
  • 24
  • 1
    this can be really slow (30 sec+), but it SHOULD always finish and shows how to not get caught on circular refs: http://danml.com/js/survey.js – dandavis Oct 17 '15 at 04:01
  • Having spent so much time on getting stringify to work, I hate to admit but your out of the box magic function seems to work! still would be nice to know how I can work around similar exception handling issues with functions I have no control of like JSON.stringify() – Saba Ahang Oct 17 '15 at 04:22
  • @dandavis can you explain what your algorithm does with circular references? I have a child object on `window` that has a lot of children in several levels, among which some might have circular references but the whole object is gone after transformation with your algorithm not only the troublesome children – Saba Ahang Oct 17 '15 at 05:38
  • the secret to it actually working (and being slow) is `u=[]` and `if (... u.indexOf(ob[z])===-1 ) { ... u.push(ob[z]);` , basically making an array of every found object and looking inside that array before continuing to follow any object property. – dandavis Oct 17 '15 at 06:12
  • @dandavis from my experience it's skipping on some of the objects, can't figure out why but the result doesn't include the child object that I know should exist – Saba Ahang Oct 17 '15 at 15:01
  • It seems to omit literal Strings cause objects with string properties are not included in the return value, it would help a great deal if you explained the function variables so that I can edit it to include string properties and objects with strings properties – Saba Ahang Oct 17 '15 at 16:10

0 Answers0