I have noticed that faceb开发者_如何学编程ook has random element IDs for every element - including elements that have no unique element id.
<div id="__w2_YvdN1r2_loading">blah</div>
Any ideas why they do this on every element & how they do this?
One reason I can think of for "why" would be to prevent, or at least reduce the incidence of, screen scraping - if the IDs are indeed random. As to how, there are inumerable ways to generate pseudo-random values and any one of them could be coerced to produce a string that resembles __w2_YvdN1r2
.
Given how Javascript heavy Facebook is, I'd be quite surprised if the IDs are as random as you think, unless the values are also output into a javascript array/object/store of some description for the pages javascript to use.
So that it's harder to scrape meaningfully, probably just a string of random chars.
I am assuming this is to reference the base64 encoded UUIDs that they may be associated with -- perhaps representing a user or token.
Giving every element a unique id guarantees that the FB can reference a particular element using scripts. For example, FB might have a script that scrolls the page to an updated element, and they want script to work for all elements. Also, this way every element can be used as a fragment identifier in a URL. It also allows you to permalink to any element in the page. So, for example, if I want to link you to the "examples" section of wikipedia's Fragment Identifier page, then I'll use the id of that element as an anchor with #
:
<a href="http://en.wikipedia.org/wiki/Fragment_identifier#examples">my link text</a>
Woah. Meta.
As for the how, we don't know because it's closed source. But FB is written in PHP so they might use something like uniqid.
精彩评论