views:

140

answers:

4

How do I get a numeric or string value for a javascript object such as window instead of the string [object]

Looking for something like an object id, like 44001 or 0xFF0012 that is unique for that object

A: 

What string representation of window would you expect to see?

Objects which provide a toString implementation to the JS runtime are responsible for their own value. So some object like HTMLElement could return, say, the innerHTML for its toString. But it doesn't, so the default is the type name.

Rex M
Looking for something like an object id, like 44001 or 0xFF0012 that is unique for that object and for object like window or opener or window.parent
mrjohn
A: 

You can write a basic debug like this, where you pass your object as o:

function debug(o){
   var r = '';
   for (var k in o){
      r += k + ' => ' + o[k] + '\n';
   }
   window.alert(r);
}

Or you can search for a dump or print_r eaquivalent like this one: http://geekswithblogs.net/svanvliet/archive/2006/03/23/simple-javascript-object-dump-function.aspx

joggink
Looking for something like an object id, like 44001 or 0xFF0012 that is unique for that object
mrjohn
I'm not sure what you're trying to achieve... If you just store the object reference you got yourself a unique identifier, no?
joggink
trying to achieve exporting the unique identifier for use somewhere else. For example knowing the specific window I was in or the unique connection I was using
mrjohn
A: 

Javascript has no native support for unique identifiers.

You can change the Object prototype to include one.

Check this thread: http://stackoverflow.com/questions/2020670/javascript-object-id

vassilis
@vassilis, I tried to make this work with the objects I am interested in and it only works for new objects I create. It does not work for say window.id() or document.id()
mrjohn
+3  A: 

Javascript doesn't keep a python-like unique hash for each object. You could create a function to assign a unique string to an object, or retrieve the string if it's already been assigned.

var getUniqueId = (function(){

  // uncomment this block if you want to avoid memory leaks
  // in browsers with crappy garbage collectors.
  /*
  try {
    window.addEventListener('unload', cleanup);
    window.addEventListener('beforeunload', cleanup);
  } catch (_) { try {
    window.attachEvent('onunload', cleanup);
    window.attachEvent('onbeforeunload', cleanup);
  } catch (__){}}

  function cleanup () { guid.knownObjects.length=0; }
  */

  guid.knownObjects=[];

  return guid;

  function guid (obj) {
    for (var i=guid.knownObjects.length; i--;) {
      if (guid.knownObjects[i][0]===obj) return guid.knownObjects[i][1];
    }
    var uid='x'+(+(''+Math.random()).substring(2)).toString(32);
    guid.knownObjects.push([obj, uid]);
    return uid;
  }

}());

Testing it out:

getUniqueId(window)
> "x7onn8ne58ug"

getUniqueId(document)
> "x9jeriqjdf9o"

getUniqueId(document)
> "x9jeriqjdf9o"

getUniqueId(window)
> "x7onn8ne58ug"

You can do console.dir(getUniqueId.knownObjects) for some useful debugging info.

getUniqueId.knownObjects
> [
  Array
  0: DOMWindow
  1: "x7onn8ne58ug"
  , 
  Array
  0: HTMLDocument
  1: "x9jeriqjdf9o"
  ]
no
Thanks this is great. Is there a way to keep the object id for say window the same when I move to another url in my domain ?
mrjohn
The only way to do it without modifying anything on the server would be by passing the info you need in the fragment identifier (the bit of text that can appear after a pound sign in the URL). You'd also need some way of including what object goes with what hash, say for example a string that, when eval'd, would result in the desired object. I'm not sure it's a good idea, though. After all, when you navigate the page, you get a new window object. It has its own address in memory and should have its own unique ID.
no
what about the history / navigator object, that is available from one request to the next, why does it not stay constant ?
mrjohn
widow.history and window.navigator appear to be the same object from page to page, but try adding a property to either one, navigating the page, and then checking for that property -- it's gone. Every object is reconstructed when the page navigates; the only way to get info to persist from one page to the next is through server-side help or URL fragment identifiers.
no
Actually there's one other approach you could take, but it's hacky... have a giant iframe that takes up the whole page, this is where the activity takes place. If the iframe and outer window are both at your domain, and the user only navigates the iframe to other pages in your domain, you can track stuff using the outer window as it won't ever navigate.
no