www.webdeveloper.com
Results 1 to 8 of 8

Thread: unique identifier for HTMLElement that works across browsers?

  1. #1
    Join Date
    May 2010
    Posts
    24

    unique identifier for HTMLElement that works across browsers?

    Any help much appreciated.

    Thank you
    Misha

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Too vague. What kind of element? Tag? Text? Comment? Attribute?

    Tag elements can be identified by:

    id - getElementById('theid')
    name - getElementsByName('thename')[position in DOM collection] - only for certain elements
    tag name - getElementsByTagName('tagname')[position in DOM collection]
    tag name with wild card - getElementsByTagName('*')[position in DOM collection]
    position as child node - childNodes[position in DOM collection]
    if first child node - firstChild
    if last child node - lastChild
    sibling position- nextSibling, previousSibling

    Can you detail and elaborate a little bit more your question?

  3. #3
    Join Date
    May 2010
    Posts
    24
    Sorry, I'll be more precise.

    Once I find an element (or any node), I want to be able to access a property access such that, if:
    Code:
    var a=x.property;
    or
    Code:
    var a=x.method();
    then I can uniquely find this element by doing something like
    Code:
    var element=x.findByUnique(a);
    Thank you!

    Misha

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You can't. At least not in this way.

    It looks like your logic is contradictorily. Once you found an element and gave it a property, that means you referred it somehow, thus why to find it again using that property as an identifier? You already have the reference of that element somewhere, otherwise you could not have given it that property, could you?

    Do I miss something? Can you exemplify your aim with a concrete HTML element?

  5. #5
    Join Date
    May 2010
    Posts
    24
    Your logic would make perfect sense except the framework I am using to execute Javascript is the Java SWT (IBM) Browser object, which does not remember objects between execute commands.

    Hence, every time, I must re-find the object.

    Somehow, references are not kept

    Thank you
    Misha

  6. #6
    Join Date
    Feb 2006
    Posts
    2,927
    If an object doesn't have an id, you can assign one the first time you look at it.
    Code:
    document.uids=1;
    document.hoozit= function hoozit(obj){
        if(obj.nodeType!=1) return '';
        if(!obj.id) obj.id= 'uid_'+document.uids++;
        return obj.id;
    }

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by misha680 View Post
    which does not remember objects between execute commands.
    Not very clear to me, but if those commands change the session, you should probably use a cookie. Or a server-side session variable. JavaScript is a client-side language - it does not keep nor track the changes between sessions.

  8. #8
    Join Date
    May 2010
    Posts
    24
    Thank you. My apologies if unclear my program is on the _browser side_ so now way (?) to set cookies on the server.

    In other words I _execute Javascript_ from the browser side to traverse/manipulate the DOM model.

    I will have to double check if assigning IDs to elements will work when finding the object... in other words, I understand the ID will be assigned, but not sure it will be retained by my next call to the Browser's "execute Javascript" command.

    This is the browser object in SWT btw if you are interested.
    http://help.eclipse.org/galileo/inde...r/Browser.html

    www.mkosh.com has the framework I am developing (Open Source) as well as a (rudimentary) Javascript Tester.

    Thank you
    Misha

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles