Question

Is there now a length limit or other changes to how Safari 5 uses javascript bookmarklets as links in Bookmarks or the bookmark bar?

The bookmarklet below works in Safari 4, but in Safari 5 I get the error "Safari can't use Javascript for this action." It doesn't seem to have anything to do with opening a new window, as the second script below works in 5 and opens a new window.

Are there other ways to use javascript for browser actions, either as bookmarklets or Safari extensions?

Update 10/07/11:

Bookmarklet now works in Safari 5 (but not 4) with one change, changing the 100% to 100%25:

javascript:s=document.getElementsByTagName('STYLE'); ex=document.getElementsByTagName('LINK'); d=window.open().document; /*set base href*/d.open();d.close(); b=d.body; function trim(s){return s.replace(/^\s*\n/, '').replace(/\s*$/, ''); }; function iff(a,b,c){return b?a+b+c:'';}function add(h){b.appendChild(h);} function makeTag(t){return d.createElement(t);} function makeText(tag,text){t=makeTag(tag);t.appendChild(d.createTextNode(text)); return t;} add(makeText('style', 'iframe{width:100%25;height:18em;border:1px solid;')); add(makeText('h3', d.title='Style sheets in ' + location.href)); for(i=0; i%3Cs.length; ++i) { add(makeText('h4','Inline style sheet'  + iff(' title="',s[i].title,'"'))); add(makeText('pre', trim(s[i].innerHTML))); } for (i=0; i%3Cex.length; ++i) { rs=ex[i].rel.split(' '); for(j=0;j%3Crs.length;++j) if (rs[j].toLowerCase()=='stylesheet') { add(makeText('h4','link rel="' + ex[i].rel + '" href="' + ex[i].href + '"' + iff(' title="',ex[i].title,'"'))); iframe=makeTag('iframe'); iframe.src=ex[i].href; add(iframe); break; } } void 0

Bookmarklet that fails in Safari 5 but works in 4:

javascript:s=document.getElementsByTagName('STYLE'); ex=document.getElementsByTagName('LINK'); d=window.open().document; /*set base href*/d.open();d.close(); b=d.body; function trim(s){return s.replace(/^\s*\n/, '').replace(/\s*$/, ''); }; function iff(a,b,c){return b?a+b+c:'';}function add(h){b.appendChild(h);} function makeTag(t){return d.createElement(t);} function makeText(tag,text){t=makeTag(tag);t.appendChild(d.createTextNode(text)); return t;} add(makeText('style', 'iframe{width:100%;height:18em;border:1px solid;')); add(makeText('h3', d.title='Style sheets in ' + location.href)); for(i=0; i%3Cs.length; ++i) { add(makeText('h4','Inline style sheet'  + iff(' title="',s[i].title,'"'))); add(makeText('pre', trim(s[i].innerHTML))); } for (i=0; i%3Cex.length; ++i) { rs=ex[i].rel.split(' '); for(j=0;j%3Crs.length;++j) if (rs[j].toLowerCase()=='stylesheet') { add(makeText('h4','link rel="' + ex[i].rel + '" href="' + ex[i].href + '"' + iff(' title="',ex[i].title,'"'))); iframe=makeTag('iframe'); iframe.src=ex[i].href; add(iframe); break; } } void 0

Bookmarklet that works with a new window:

javascript:var MTl=document.getElementsByTagName('script');for(var MTi=0,MTr,MTa=[];MTl[MTi];MTi++){if(MTr=MTl[MTi].getAttribute('src')){MTa[MTa.length]=MTl[MTi];}}if(!MTa.length){alert('This page does not use any external scripts');}else if(MTa.length==1){void(window.open(MTa[0].getAttribute('src')));}else{var MTw=window.open('');MTw.document.open();MTw.document.write('<html><head><title>External scripts for '+document.title+'<\/title><\/head><body><h1>External scripts for '+document.title+'<\/h1><p>');for(MTi=0;MTa[MTi];MTi++){MTw.document.write('<a href=\''+MTa[MTi].getAttribute('src')+'\'>'+(MTa[MTi].id?MTa[MTi].id:MTa[MTi].getAttribute('src').replace(/\/$/,'').replace(/^.*\//,''))+'<\/a><br>');}MTw.document.write('<\/p><\/body><\/html>');MTw.document.close();}
Was it helpful?

Solution

I saw the same error from one of my bookmarklets. It turned out that it was improperly URL-encoded all along, and Safari 5.1 must just be more strict about that than previous versions. It looks like you have the same problem. Replace literal '%' characters with '%25' and see if you have better luck. (For example, change "width:100%;" to "width:100%25;".)

Too bad there's such a cryptic error message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top