views:

144

answers:

4

Hi, Is it possible to use "user defined attribues" in html/xhtml tags? Best regards.

A: 

I don't think you can really define/use "custom attributes" -- and, even if you could : how browsers would know what to do with those ?

You probably can inject whatever you want in the "XHTML" document ; but it will not be XHTML valid anymore, I'd say

Pascal MARTIN
Hi,I need my further using.Thanks.
+6  A: 

In HTML as it stands? No.

In XHTML? Kinda. You have to put them in their own namespace and then not serve the document as text/html (which rather excludes Internet Explorer)

In HTML 5? The current draft supports author defined attributes providing they are prefixed with data- and used only internally.

David Dorward
A: 

Use Javascript and define the attribute after load. This way you still have valid HTML.

If you'd like to keep data for an attribute take a look into javascript libraries such as jQuery. Which adds a data() method:

$("div").data("test", { first: 16, last: "pizza!" });
$("span:first").text($("div").data("test").first);
$("span:last").text($("div").data("test").last);
benvds
Thanks...That is very useful information for me. I already use JQuery but I don't know so well.Thanks too much... Best regards.
+1  A: 

I don't know if it is in specifications, but Yes, you can do that in both HTML and XHTML and all browsers will perfectly understand that. For example:

<html>
<head>
<script>
window.onload=function() {
    alert(document.getElementById("data").getAttribute("somenamespace:somevariable"));
};
</script>
</head>
<body>
<div id="data" somenamespace:somevariable="hello world" />
</body>
</html>

This code perfectly works in ALL browsers including IE5.5

Max
Thanks very much...Best regards.
Is it possible to use without somenamespace: ... for example like div id="data" var="hello world" /> ?Best regards.
Yes you can. However I would recommend using namespaces (you can call it whatever you like) to separate your properties from standard properties and/or some additional library properties. It will just be easier in future to determine what belongs where if everything has proper namespaces.For example, if your library changes colors on forms, you can call the attributes "colorfactory:redness = 5"
Max