Saturday, February 5, 2011

Getting jQuery to work in Jetpack

Hi, I am experimenting with Jetpack and I would like to parse all the years in a given html page and then wrap the year with a link to the Wiki page. I tried the code in jquery and there it works but now I am using it in Jetpack and it gives an error $(doc).replace is not a function. I am definitely new to Jquery / Jetpack so maybe I am missing something really easy but your help is much appreciated.

EDIT: I have tried the suggestions but I am still stuck. The weird thing is that this

JQuery function works:

    (function($) {
        $.fn.clickUrl = function() {  
                var regexp = /([1-2][0-9][0-9][0-9])/gi;
          this.each(function() {
          $(this).html(
                $(this).html().replace(regexp,'<ahref=\"http://nl.wikipedia.org/wiki/$1\">$1<\/a>')
          );
         });
        return $(this);
        }
    })(jQuery);

and basically, I would like to 'port' this function to Jetpack.

Thanks a lot for any suggestion! Best regards, Diederik

this is the 'old' non-working port of my JQuery function to Jetpack:

jetpack.statusBar.append({
html: "Hyperlink Years",
width: 80,
onReady: function(widget){
$(widget).click(function(){
var regexp = /([1-2][0-9][0-9][0-9])/gi; var doc = jetpack.tabs.focused.contentDocument; $(doc).each(function() { $(this).html( $(doc).replace(regexp,'$1<\/a>')); }); return $(doc); });
}

});

  • I'm not familiar with jetpack, but your jquery seems to be quite messed up.

    If "doc" is an HTML document, then doing $(doc).each() doesn't really make sense. It would only loop once, and "this" would be the same as doc.

    Then later you are doing $(doc).replace(regexp, ...), but replace() is not a jquery function. You might have wanted to do .html().replace(regexp, ...); HOWEVER, I do not recommend doing this because it will not work - you will just end up replacing any numbers in the document, even if they are part of another URL or the HTML of the page.

    For more information, refer to this question or google for jquery text nodes: http://stackoverflow.com/questions/926580/find-text-string-using-jquery/926881#926881

    From BarelyFitz

0 comments:

Post a Comment