最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the best practice for writing bookmarklets - Stack Overflow

programmeradmin0浏览0评论

I am writing some bookmarklets for a project that I am currently working on and I was wondering what the best practice for writing a bookmarklet was. I did some looking around and this is what I came up with

 javascript:void((function()
  {
    var%20e=document.createElement('script');
    e.setAttribute('type','text/javascript');
    e.setAttribute('src','.js');
   document.body.appendChild(e)
  })())

I felt this is nice because the code can always be changed (since its requested every time) and still it acts like a bookmarklet. Are there are any problems to this approach ? Browser inpatibility etc? What is the best practice for this?

I am writing some bookmarklets for a project that I am currently working on and I was wondering what the best practice for writing a bookmarklet was. I did some looking around and this is what I came up with

 javascript:void((function()
  {
    var%20e=document.createElement('script');
    e.setAttribute('type','text/javascript');
    e.setAttribute('src','http://someserver./bookmarkletcode.js');
   document.body.appendChild(e)
  })())

I felt this is nice because the code can always be changed (since its requested every time) and still it acts like a bookmarklet. Are there are any problems to this approach ? Browser inpatibility etc? What is the best practice for this?

Share Improve this question asked Dec 19, 2009 at 13:23 Ritesh M NayakRitesh M Nayak 8,06314 gold badges51 silver badges78 bronze badges 2
  • This article provides a bookmarklet template which looks good to me latentmotion./how-to-create-a-jquery-bookmarklet – Phil Hale Commented Jul 8, 2011 at 13:07
  • Here's an alternative link I've used before. benalman./code/test/jquery-run-code-bookmarklet – Phil Hale Commented May 28, 2013 at 9:13
Add a ment  | 

2 Answers 2

Reset to default 7

That bookmarklet will append a new copy of the script to the document every time it is run. For long-lived pages (e.g. Gmail), this could add up to a lot of memory usage, and if loading your script has side effects, they’ll occur multiple times. A better strategy would be to give your script an id, and check for existence of that element first, e.g.:

var s = document.getElementById('someUniqueId');
if (s) {
  s.parentNode.removeChild(s);
}
s = document.createElement('script');
s.setAttribute('src', 'http://example./script.js');
s.setAttribute('type', 'text/javascript');
s.setAttribute('id', 'someUniqueId');
document.body.appendChild(s);

N.B. another alternative is to keep the existing script if it’s already in the document. This might save some server traffic if your bookmarklet is used frequently between page reloads. The worst case is that someone is using an older version of your script for a while; if you don’t expect it to change often, that might be fine.

Looks OK. But if your js file is already cached, it will not be requested every time. So you'd need it to append '?' + new Date() to your src attribute to ensure it is requested every time.

发布评论

评论列表(0)

  1. 暂无评论