通过小书签将jQuery,Colorbox和自定义代码注入页面
问题描述:
我一直受Instapaper bookmarklets的启发,它允许将页面添加到Instapaper而无需单独的弹出窗口,也无需重新加载整页。我想将这个想法与Delicious bookmarklet结合为书签页面。通过小书签将jQuery,Colorbox和自定义代码注入页面
jQuery Colorbox plugin支持将iframe内容加载到colobox弹出框中,所以它看起来是一个很好的开始。我拼凑了几个小书签,以获得下面的代码,这似乎是一个良好的开端,直到我添加“$.fn.colorbox ...”行。我似乎无法打开Colorbox。我甚至试图简化它,只是打开谷歌的希望页,但没有运气那里:
javascript:
function iprl5(){
var d=document,z=d.createElement('scr'+'ipt'),y=d.createElement('scr'+'ipt'),x=d.createElement('scr'+'ipt'),b=d.body,l=d.location,t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');
try{
if(!b)
throw(0);
d.title='(Saving...)%20'+d.title;
x.setAttribute('src','http://cachedcommons.org/cache/jquery/1.4.2/javascripts/jquery.js');
y.setAttribute('src','http://cachedcommons.org/cache/jquery-colorbox/1.3.9/javascripts/jquery-colorbox.js');
b.appendChild(x);
b.appendChild(y);
$.fn.colorbox({href:'http://www.delicious.com/save?url='+encodeURIComponent(window.location.href)+'&title='+encodeURIComponent(document.title)+'&v=5&',open:true,iframe:true});
}
catch(e){
alert('Please%20wait%20until%20the%20page%20has%20loaded.');
}
}
iprl5();
void(0)
答
脚本加载在一个单独的线程。您需要等到脚本加载完成后再使用它。通常是这样的:
var checkIfLoaded = function(){
if($.fn.colorbox){
// continue your processing
}else{
window.setTimeout(200, checkIfLoaded);
}
}
checkIfLoaded();