"site" element attribute - what is it?
I开发者_JAVA百科 am looking over some site code for a review and have noticed the attribute "site" being used within an <ul>
, for example:
<li site:bluray="154" site:ds="91" site:dvd="345" site:pc="77" site:ps3="248" site:psp="49" site:wii="153" site:xbox360="216" ><a href="#">In Stock</a></li>
I can'y find any resources to explain what this is - does anyone know what doing the above actually does?
Many thanks
Those are custom attributes added (probably by dynamic server side code) to the <li>
elements.
It doesn't do anything by itself, it's the same like giving name or id to elements - some data that may or may not be used by some other code.
Good guess would be that the website have some client side script that identify the click on the nested link inside the list item, take the data out of the list item element and build the proper URL or send the proper AJAX request to the server or whatever it's doing with the data.
Looking into that client side script will give you better chance to understand the real purpose behind those attributes. :)
Edit: in your case, the JS file http://media1.shopto.net/scripts/site.js is using those attributes to build the href
of the links on the fly, here is the relevant block of code from that file:
// The current href on each link needs to have the platform pre-pended.
// So store the current href before we change it.
link.submenu.getElements('ul.category a').each(function(sublink) {
var count = sublink.getParent().get('site:'+platform.replace('%20',''));
if( count==null && !sublink.getParent().get('site:all') )
{
sublink.getParent().removeChild(sublink);
} else {
sublink.set('href', href + sublink.get('href').replace('#',''));
/*
if( count!=null )
{
sublink.set('text',sublink.get('text') + ' (' + count + ')');
}
*/
}
});
The colon in the attribute name gives it away - it is almost certainly related to an XML namespace.
If you look at the top of the HTML page, you should notice a couple of things:
Firstly, it should have a doctype identifying it as XHTML.
Secondly, it should have an XML namespace identifier (xmlns) for the site
namespace. This will confirm for certain that this is what it is.
It is possible to add any attribute you like to an HTML element. It's non-standard, and in some cases you may find the browser doesn't like it - for example, if your doctype is XHTML, the browser is supposed to enforce that you only use the allowed standard attributes.
Using an XML namespace for additional attributes is a common way around this limitation. In fact, in XHTML, it's what you're supposed to do.
In the future, you should see less of this, as more sites move to HTML5 and away from XHTML. HTML5 does away with all of this by going back to a more free-form and relaxed attitude to custom attributes. The HTML5 spec says that you can use any attribute you like in your elements. If you intend to use custom attributes then you should prefix them with data-
, which helps distinguish them from real attributes, but you don't need to worry about namespaces any more.
The site
is just a namespace prefix as per XML Namespaces:
In the data model implied by XML, an XML document contains a tree of elements. Each element has an element type name (sometimes called the tag name) and a set of attributes; each attribute consists of a name and a value. Applications typically make use of the element type name and attributes of an element in determining how to process the element. In XML 1.0 without namespaces, element type names and attribute names are unstructured strings using a restricted set of characters, similar to identifiers in programming languages. I'll call these names local names. This is problematic in a distributed environment like the Web. One XML document may use
part
elements to describe parts of books, another may usepart
elements to describe parts of cars. An XML application has no way of knowing how to process a part element unless it has some additional information external to the document.The XML Namespaces Recommendation tries to improve this situation by extending the data model to allow element type names and attribute names to be qualified with a URI. Thus a document that describes parts of cars can use
part
qualified by one URI; and a document that describes parts of books can usepart
qualified by another URI. I'll call the combination of a local name and a qualifying URI a universal name. The role of the URI in a universal name is purely to allow applications to recognize the name. There are no guarantees about the resource identified by the URI. The XML Namespaces Recommendation does not require element type names and attribute names to be universal names; they are also allowed to be local names.
So <cars:part xmlns:cars="http://www.cars.com/xml"/>
maps to
<{http://www.cars.com/xml}part/>
The use of cars
(or site
) as the prefix is just for convenience. And since the xmlns:
pseudo-attribute is inheritable, it doesn't need to be included in the element if an ancestor has defined the prefix already.
Read more about XML namespaces on Wikipedia.
精彩评论