Live data from Hacker News

Show HN:jQuery Signature - Getting a unique signature for a dom element

dokeeno.com

11–14 of 14 posts

Re: Show HN:jQuery Signature - Getting a unique signature for a dom element

#11
post #10
post #8

Earlier quoted context omitted.

Thanks. First one seems nice. I'll implement that. But if the content is changed dynamicly we might face some issues. BTW: in reality developers do not adhere to [2] and there is no any browser restrictions for that. So I have to think beyond the spec.

> But if the content is changed dynamicly we might face some issues. Regardless the problem I was raising, if the document changes dynamically, your generated signature has great chances to fail (cf. also comment from bdunn). > in reality developers do not adhere to [2] I really hope it's not true because it would really be quite pointless. Let's try by yourself: put the same id name (e.g. "testId") on two different…

>Regardless the problem I was raising, if the document changes dynamically, your generated signature has great chances to fail (cf. also comment from bdunn).

Yes :)

>It will return only one element. So you'll never be able to find the second one with a selector...

Thanks. I was not aware of that. Now this encourage me even more :) If a bad programmer like me place two ids with same name. Extension developer like x did not able to get the second one. That why I need this

Re: Show HN:jQuery Signature - Getting a unique signature for a dom element

#12
post #6
post #5

Earlier quoted context omitted.

You have good valid point here. for the first one it's kind of a problem. Do you have any sugessions. For the second one, answer is simple we may have several elements with same id but in different parents. That's why we need something like this. This is not 100% unique but a innocent try :)

For the first problem, you could test the generated selector to verify it matches only one element. If not you can use, for example, the nth-child pseudo-class to select the right element. For the second one, specs say that the "id" value must be unique in the entire document [2]. [1] http://www.w3.org/TR/selectors/#structural-pseudos [2] http://www.w3.org/TR/html401/struct/global.html#h-7.5.2

updated the source according to the first recommendation.

Re: Show HN:jQuery Signature - Getting a unique signature for a dom element

#13
I just wrapped up some code I've used in similar situations:

https://github.com/rcs/jQuery.relativeSelector

Instead of going all the way to the body, this will find a selector that works given the structure of the page.

For instance, a page like:

  Target!
you can get "#firstdiv" back.

For something like

  Target!
You can get "#firstClass div:eq(0)".
Post reply on HN