开发者

JQuery-generated bookmarklet not running on some websites

I've searched around, but can't find an answer to this.

I've used the excellent bookmarklet tutorial at http://www.smashingmagazine.com/2010/05/23/make-your开发者_如何学编程-own-bookmarklets-with-jquery/ which shows how to use JQuery to throw up an iframe. It appends a block to the current page, checks for JQuery, and then fires off an iframe using jQuery commands. It works brilliantly, but I happened upon posterous.com, where it's not working.

To demonstrate, go to the tutorial (http://www.smashingmagazine.com/2010/05/23/make-your-own-bookmarklets-with-jquery/), find the "WikiFrame" bookmarklet (about half way down the page), drag it to your browser toolbar, and try it out on another site by selecting some text and clicking the bookmarklet. It works great - it pops up a iframe of a page of Wikipedia.

However, on posterous.com, it does nothing. The script block gets added to the page, but it doesn't fire off.

Anyone any thoughts on why?


There is a bug in the wikiframe bookmarklet from that tutorial.

It tests to see if the jQuery object exists, but then goes on to use $. On posterous.com, $ is not equal to jQuery.

 if (typeof jQuery == 'undefined') {
      //...
 } else {
      runthis();
 }

 function runthis() {
     if ($("#wikiframe").length == 0) {
          //...

The solution is to use jQuery instead of $, or to alias $ to jQuery using something like

(function($){ ... })(jQuery)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜