开发者

What tag can I use to store information in a XHTML document HEAD that is ignored by browsers?

I need to store some arbitrary XML data in an XHTML HEAD element, that will be ignored by browsers. A little like an element version of the "data-*" mechanism for making up your own attributes. Is there a standards-compliant way to do this?

edit: Some people asked why I would want to do this. Basically I'm building a service that processes web pages, and I want the creator of the web page to be able to pass optional "hints" to this 开发者_C百科service to tell it how to parse the page. I haven't yet decided exactly what the hints will be, so I want to keep that fairly flexible.

My code already uses the Java JSoup library to parse the XHTML, so I thought it would be nice if the same library could parse the "hint" information, rather than having to parse it separately.


Have you tried putting the information in a meta element? For example you might try this:

<meta name="blammy" content="&lt;blah blam=&quot;my&quot;&gt;">

Edit Check out this article on Dev Opera. Item 13 (scroll down some on the page) addresses this.


I have found the following article about very original idea of data storage in HTML comments, which are ignored by all browsers. It is in Russian, but Google Translate will help you :).
http://www.manhunter.ru/webmaster/322_ispolzovanie_html_kommentariev_dlya_hraneniya_dannih.html


https://developer.mozilla.org/en/Using_XML_Data_Islands_in_Mozilla

Internet Explorer has an "XML Data Islands" feature that allows Web authors include XML data inline in HTML documents using the <xml> tag. This feature is not based on multi-vendor Web standards and is not supported in Firefox (or other non-IE browsers). However, HTML5 has a more general feature called "data blocks" that can carry almost any textual data, including XML.

A <script> when it does not have a src attribute and has a type attribute that does not identify an executable script type. The content of the <script> element is then a data block that JavaScript can use. When putting XML in a data block, you need to make sure that the XML content you are embedding does not have an end tag that case-insensitively matches "</script>".

For example, a simple XML purchase order can be embedded like this:

<script id="purchase-order" type="application/xml">
<purchaseOrder xmlns="http://example.mozilla.org/PurchaseOrderML">
  <lineItem>
    <name>Line Item 1</name>
    <price>1.25</price>
  </lineItem>
  <lineItem>
    <name>Line Item 2</name>
    <price>2.48</price>
  </lineItem>
</purchaseOrder>
</script>

The XML source text can then be retrieved like this:

var orderSource = document.getElementById("purchase-order").textContent;

The XML source text can be parsed into a DOM tree using the DOMParser API:

var parser = new DOMParser();
var doc = parser.parseFromString(orderSource, "application/xml");

The HTML5 data block-based way shown here works in Firefox, in Opera, in WebKit-based browsers such as Chrome and Safari, and in IE9 while IE's XML data islands work only in IE.


There are a few possible methods:

1 - Store simple information in a <meta> element

<meta name="yourApp-NamedElement" content="simple_info" />

I am unsure what the max length of the content field could be. Theoretically you could push all of your encoded XML data into one meta element. But, you would then need to decode the characters and then parse as XML:

<meta name="yourApp-Config" content="&lt;xml&gt;&lt;config&gt;&lt;user&gt;123&lt;/user&gt;&lt;foo&gt;bar&lt;/foo&gt;&lt;/config&gt;&lt;/xml&gt;" />

2 - Using CDATA within a <script> block

<script type="text">
//<![CDATA[
    <arbitrary>
        <markup>blahblah</markup>
    </arbitrary>
//]]>
</script>

CDATA is used to tell whatever is parsing the document that you have arbitrary character data.

3 - Using an <object> block with <params>

<object classid="foobar">
    <param name="something" value="something's value" />
    <object classid="foobarA">
        <param name="foobar2sbaseparam" value="blahblahlbah" />
        <object classid="foobarA1">
            <param name="sortofnested" value="startstogetunwieldy" />
            <param name="a123" value="barbar" />
        </object>
    </object>
</object>

I do not like this solution, but nevertheless, objects are allowed under XHTML strict and can be nested within one another (<params> may not be nested).

However, is there a good reason why you need to have your xhtml validated? There are some who would argue that validation is a useful debugging tool, rather than a requirement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜