我一直在为此烦恼,我真的希望有人知道这个问题并找到解决问题的办法......
我正在生成一个书签,它完美无缺,神奇地说,如果我在MYSQL和PHP上研究了这么多天之后我可以这样说。
例如,请考虑生成此bookmarklet。
javascript:(function(){ a=document.createElement('script'); a.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendChild(a); data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","Ranjan","text"],["#txtmother","Neelam","text"],["#txtPincode","452010","text"],["#txtPhone","2147483647","text"],["#txtEmail","aayush@mail.com","text"],["#rdosex_0,#rdosex_1","1","radio"]]'; for(a=$.parseJSON(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] =='text') { $(c[0]).val(c[1]); } else if (c[2] == 'radio'){ rdata = c[0].split(','); rnum = c[1] - 1; select = rdata[rnum]; $(select).attr('checked', true); } } })();
这是一个关闭编译的书签的重新创建,我无法找到原始,因为它从未得到保存(长篇故事..)所以我想我也可以编辑闭包编译一个。 正在插入JSON文件,因为必须动态生成bookmarklet并且注定要在HTTPS网站上运行,因此无法发送AJAX调用。
无论如何,代码完美无缺,但当我尝试将其作为超链接插入时,它会中断。 如果需要,代码几乎就是这样的
"<a href=" + (Code that was shown above) + "> Drag me to your bookmarks bar!</a>"
它打破..如果需要,这是打印出来的源代码。
<span class='span6'>Please drag the following button to your bookmarks bar! <br>javascript:(function(){ a=document.createElement('script'); a.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendChild(a); data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","Ranjan","text"],["#txtmother","Neelam","text"],["#txtPincode","452010","text"],["#txtPhone","2147483647","text"],["#txtEmail","aayush@mail.com","text"],["#rdosex_0,#rdosex_1","1","radio"]]'; for(a=$.parseJSON(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] =='text') { $(c[0]).val(c[1]); } else if (c[2] == 'radio'){ rdata = c[0].split(','); rnum = c[1] - 1; select = rdata[rnum]; $(select).attr('checked', true); } } })();<a href='javascript:(function(){ a=document.createElement('script'); a.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendChild(a); data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","Ranjan","text"],["#txtmother","Neelam","text"],["#txtPincode","452010","text"],["#txtPhone","2147483647","text"],["#txtEmail","aayush@mail.com","text"],["#rdosex_0,#rdosex_1","1","radio"]]'; for(a=$.parseJSON(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] =='text') { $(c[0]).val(c[1]); } else if (c[2] == 'radio'){ rdata = c[0].split(','); rnum = c[1] - 1; select = rdata[rnum]; $(select).attr('checked', true); } } })();'>Drag me!</a></span>
它打破了很多层次,我已多次尝试绕过它,但每次我做一个小改动它再次打破。我真的希望有一个更可靠的解决方案,如果有人可以帮助我,我会非常感激..
编辑:这是用htmlspecialchars转义双引号后发生的事情
<span class='span6'>Please drag the following button to your bookmarks bar! <br>javascript:(function(){ a=document.createElement('script'); a.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendChild(a); data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","Ranjan","text"],["#txtmother","Neelam","text"],["#txtPincode","452010","text"],["#txtPhone","2147483647","text"],["#txtEmail","aayush@mail.com","text"],["#rdosex_0,#rdosex_1","1","radio"]]'; for(a=$.parseJSON(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] =='text') { $(c[0]).val(c[1]); } else if (c[2] == 'radio'){ rdata = c[0].split(','); rnum = c[1] - 1; select = rdata[rnum]; $(select).attr('checked', true); } } })();<a href='javascript:(function(){ a=document.createElement('script'); a.setAttribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendChild(a); data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","Ranjan","text"],["#txtmother","Neelam","text"],["#txtPincode","452010","text"],["#txtPhone","2147483647","text"],["#txtEmail","aayush@mail.com","text"],["#rdosex_0,#rdosex_1","1","radio"]]'; for(a=$.parseJSON(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] =='text') { $(c[0]).val(c[1]); } else if (c[2] == 'radio'){ rdata = c[0].split(','); rnum = c[1] - 1; select = rdata[rnum]; $(select).attr('checked', true); } } })();'>Drag me!</a></span>
答案 0 :(得分:1)
将其作为超链接插入时中断的原因是需要对某些字符(尤其是空格'
和"
)进行编码。
有关详细信息,请参阅Does a JavaScript bookmarklet need to be a valid URL?。
答案 1 :(得分:1)
请使用
document.getElementsByTagName('head').item(0).appendChild(a);
而不是你使用的
document.body.appendChild(a);
我希望它能运作良好。