Search code examples
jqueryyui

Is html source code could be a selector in YUI like jQuery? Finding title and meta description by YUI


I am new in YUI3 and trying some stuff similar to jquery, I am a jQuery guy and don't know to
how to achieve the same in YUI, which i am doing below in jQuery,

<script language="javascript" src="http://code.jquery.com/jquery-latest.js" >
</script>
 <script language="javascript">
$(function(){
    var data = '<html xmlns="http://www.w3.org/1999/xhtml"><head>'+
               '<title>This is title</title> <meta name="description" content="This is description" /> </head><body></body></html>';
    var title=(/<title>(.*?)<\/title>/m).exec(data)[1];
    console.log(title); //output: This is title
    var dom_list = $(data).find("meta").prevObject;
    $.each(dom_list, function(i,v){
        var c = $(v).attr('content');
        var n = $(v).attr('name');
        if(n!=undefined){
            if(n.toLowerCase()=='description'){
                desc = c;
            }
        }
    }); 
    console.log(desc); // output : This is description
});
</script>

Question: As data variable is the html source code and it is a selector in jquery and i
can find meta tag in this, So, can i have the same in YUI?

Any suggestion would be appreciable.


Solution

  • var data = '<html xmlns="http://www.w3.org/1999/xhtml"><head>' + '<title>This is title</title> <meta name="description" content="This is description" /> </head><body></body></html>';
    var title = (/<title>(.*?)<\/title>/m).exec(data)[1];
    console.log(title); //output: This is title
    
    var metaList = Y.Node.create(data).all("meta");
    metaList.each(function(node) {
        var c = node.get('content'),
            n = node.get('name');
        if (n !== null) {
            if (n.toLowerCase() === 'description') {
                desc = c;
            }
        }
    });
    console.log(desc); // output : This is description
    

    Can view a runnable example here: http://jsfiddle.net/brianjmiller/LTKs6/

    You may also be interested in http://jsrosettastone.com which shows common translations between YUI and jQuery.