Quietly extract JSON metadata contained within DOM elements.
Basic Usage
1. Include the scripts required:
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-min.js"></script> <script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/dom/dom-min.js"></script> <script type="text/javascript" src="metadata-min.js"></script>
2. Store the data in DOM elements
<p id="one" class="someclass {itemid: 1, itemlabel: 'Label 1'}">This is p1</p> <p id="two" class="someclass" data="{itemid: 2, itemlabel: 'Label 2'}">This is p2</p> <p id="three" class="someclass"><script type= "text/javascript">{itemid: 3, item_label: 'Label 3'}</script>This is p3</p>
3. Retrieve using metadata
var dom = YAHOO.util.Dom; var metadata = YAHOO.plugin.Metadata;metadata.setType("class"); document.write( metadata.get( dom.get("one") ).itemid + ", " + metadata.get( dom.get("one") ).itemlabel + "<br />");
metadata.setType("attr", "data"); document.write( metadata.get( dom.get("two") ).itemid + ", " + metadata.get( dom.get("two") ).itemlabel + "<br />");
metadata.setType("elem", "script"); document.write( metadata.get( dom.get("three") ).itemid + ", " + metadata.get( dom.get("three") ).itemlabel + "<br />");
Using YUI Loader
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yuiloader/yuiloader-min.js"></script>
<script type="text/javascript">var loader = new YAHOO.util.YUILoader({ onSuccess: function() { var dom = YAHOO.util.Dom; var metadata = YAHOO.plugin.Metadata; // do something with metadata } }); loader.addModule({ name: "metadata", type: "js", fullpath: "http://yui-metadata.googlecode.com/svn/trunk/yui-metadata/metadata-min.js", varName: "YAHOO.plugin.Metadata", requires: ['yahoo', 'dom'] }); loader.require("metadata"); loader.insert();
</script>
Documentions and demo
Please view the source code of this link: http://labs.xddnet.com/yui-metadata/example/example.html
Contact
Feedback and suggestions are always welcome, you can reach me at tszming at gmail dot com.
Comments