I was wondering if I can create a custom HTML tag with the same functionality of Facebook's Open Graph meta tags for my site.

I was thinking of something like this:

<mysite title="My website title" description="Some description" 
    image="http://mysite.com/image.png">

And then use PHP to fetch this data (I can already do this with meta-tags).

Some points:

  • I use HTML5, are there any syntax problems using this method?

  • Google+ uses something like this: g:plusone

有帮助吗?

解决方案 2

Marat Tanalin is right, in HTML5 you are not allowed to "invent" elements/attributes/values that are not specified. Alohci gives a nice example why that is the reason.

mmmshuddup notes, that you could use XHTML5. However, I think it would be allowed in that case, if you extend the vocabulary formally correct. It's XML, after all. It couldn't be a polyglot (X)HTML5 document anymore, though.

I think there may be a (X)HTML5 solution that could work for you (depends on your specific use case, though): the data-* attribute:

<div data-title="My website title" data-description="Some description" data-image="http://mysite.com/image.png">…</div>

You can "invent" attributes that start with data- followed by a string you are free to define.

Another possible (more complex) way might be to use microdata with an existing vocabulary that suits your needs (if you can't find one, you could create one yourself). I would only go this way if the (meta)data you want to provide is valuable for others.

其他提示

Main issue is that custom elements are formally invalid.

Here's what the docs say:

Authors must not use elements, attributes, or attribute values that are not permitted by this specification or other applicable specifications, as doing so makes it significantly harder for the language to be extended in the future.

HTML5 Elements

By the way, there was a solid future-proof proposal as for custom elements in W3 bug-tracker, but it has been rejected by a cool guy Hixie as usual.

Yes you should be able to do this if you render the page as XHTML (XML). Using <html xmlns="http://www.w3.org/1999/xhtml"> in your opening <html> tag. As Marat Tanalin said, it's "formally" invalid syntax.

You can parse the document various ways using simplexml_load_string() in use XPATH to query for those elements.

$fileContents = file_get_contents('/path/to/template', FILE_USE_INCLUDE_PATH);
$template     = simplexml_load_string($fileContents);
$myTags       = $template->xpath('//mysite');

See this question: Removing Chrome's "translate" DOM Property

Here, someone has used a non-standard attribute name (non-standard element names have potentially the same problem) only to find that the name has now been standardized and interpreted in browsers, and the questioner is faced with a horrendous work-around to avoid a massive amount of rework.

I've never seen a use case that justifies the risks involved.

Of course, if you happen to be as big as Google or Facebook, your non-standard name is likely to be well enough known to not be caught out by name clashes.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top