IE6 - IE8 javascript error... help?
I'm making a bookmarklet to view cookies for any site, it works in every browser from IE9+, firefox, chrome, safari, etc. I don't care about IE5.5, so the main issue is the javascript error on IE6 - IE8. They error saying "Unterminated string constant" from the following:
javascript:if(document.getElementById('cookieelement')!=null){
document.body.removeChild(document.getElementById('cookieelement'));
} else {
document.bo开发者_JAVA技巧dy.innerHTML='<div style="font-size:10pt;color:#000000;padding:10px;border:solid 1px #000000;background:white;display:block;position:fixed;top:0px;z-index:999999999999;" id="cookieelement"></div>'+document.body.innerHTML;
cookieelement=document.getElementById('cookieelement');
cookies=document.cookie.split(';');
cookiehtml=('<table style="border-collapse:collapse;border:Solid 1px #000000;">\n');
for(i=0;i<cookies.length;i++){
cookie=cookies[i];
name=cookie.substr(0,cookie.indexOf('='),cookie.length);
val=cookie.substr(cookie.indexOf('=')+1,cookie.length);
cookiehtml+=('\t<tr>\n\t\t<td style="padding:3px;text-align:right;padding-right:10px;border:Solid 1px #000000;">'+name+'</td>\n\t\t<td style="padding:3px;text-align:left;border:Solid 1px #000000;">'+val+'</td>\n\t</tr>\n');
}
cookiehtml+=('</table>');cookieelement.innerHTML=cookiehtml;
}
void(0);
I do not get a unterminated string content error in IE7. However, I got an equally cryptic object does not support property or method message.
This could be because in IE it is possible to access elements by their id
globally. Meaning if you have a element with id abcd
, you can access it via document.abcd
globally. this is not standard, but possible in IE.
Try changing the name of your cookie element variable to something else. I changed it to cookieElement1
, and voila, it works:
javascript: if(document.getElementById('cookieelement')!=null){ document.body.removeChild(document.getElementById('cookieelement')); } else { document.body.innerHTML='<div style="font-size:10pt;color:#000000;padding:10px;border:solid 1px #000000;background:white;display:block;position:fixed;top:0px;z-index:999999999999;" id="cookieelement"></div>'+document.body.innerHTML; cookieelement1=document.getElementById('cookieelement'); cookies=document.cookie.split(';'); cookiehtml=('<table style="border-collapse:collapse;border:Solid 1px #000000;">\n'); for(i=0;i<cookies.length;i++){ cookie=cookies[i]; name=cookie.substr(0,cookie.indexOf('='),cookie.length); val=cookie.substr(cookie.indexOf('=')+1,cookie.length); cookiehtml+=('\t<tr>\n\t\t<td style="padding:3px;text-align:right;padding-right:10px;border:Solid 1px #000000;">'+name+'</td>\n\t\t<td style="padding:3px;text-align:left;border:Solid 1px #000000;">'+val+'</td>\n\t</tr>\n'); } cookiehtml+=('</table>');cookieelement1.innerHTML=cookiehtml; } void(0);
精彩评论