jquery - How do I get data from the YouTube api on a page loaded by ajax? -


i'm using following code me view count specific youtube video , place in div id: ytviews:

var video_id='grcglk6izz0'; $.getjson('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data){ document.getelementbyid('ytviews').innerhtml = (data.data.viewcount); }); 

example

here's problem:

the div placing view count in resides on page loaded via ajax. when visit page when loaded ajax view count doesn't show.

here's ajax code:

$(function () {     var b = "",         = $("#main-content");     $("#page-wrap");     $(document).delegate(".dyn a", "click", function () {         window.location.hash = $(this).attr("href");         return !1     });     $(window).bind("hashchange", function () {         string.prototype.totitlecase = function (b) {             var = this;             1 !== b && (a = a.tolowercase());             return a.replace(/\b[a-z]/g, function (a) {                 return a.touppercase()             })         };         b = window.location.hash.substring(1);         document.title = window.location.hash.replace("#", "").replace(/[_]/g, " ").replace(".html", "").replace("and", "+").totitlecase();         b && a.find("#guts").fadeout(200, function () {             a.hide().load(b + " #guts", function () {                 a.fadein(200);                 $(".dyn a").removeclass("current");                 $(".dyn a[href=" + b + "]").addclass("current")             })         })     });     $(window).trigger("hashchange") }); 

i had same issue facebook likes solved using following code allow fbml re-parsed after ajax content has been inserted dom:

$(document).ajaxcomplete(function(){     try{         fb.xfbml.parse();      }catch(ex){} }); 

i have tried putting view count code within ajaxcomplete solution, kinda worked except makes facebook links go crazy , flash in page.

could me understand how can make both load after ajax content has been loaded.

thanks in advance!

in load callback, can add $.getjson:

    ...     b && a.find("#guts").fadeout(200, function () {         a.hide().load(b + " #guts", function () {             a.fadein(200);             $(".dyn a").removeclass("current");             $(".dyn a[href=" + b + "]").addclass("current");             $.getjson('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data) {                   $('#ytviews').html(data.data.viewcount);             });         })     })     ... 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -