0

我已经为此烦恼了很长时间,我真的希望有人知道这个问题并找到解决它的方法..

我正在生成一个书签,它完美地工作,如果我在 MYSQL 和 PHP 上磨蹭了这么多天之后可以这么说的话。

例如,请考虑生成此书签。

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 文件是这样插入的,因为小书签必须动态生成,并且注定要在 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='[[&quot;#txtapplicantlname&quot;,&quot;agrawal&quot;,&quot;text&quot;],[&quot;#txtapplicantfname&quot;,&quot;aayush&quot;,&quot;text&quot;],[&quot;#txtfather&quot;,&quot;Ranjan&quot;,&quot;text&quot;],[&quot;#txtmother&quot;,&quot;Neelam&quot;,&quot;text&quot;],[&quot;#txtPincode&quot;,&quot;452010&quot;,&quot;text&quot;],[&quot;#txtPhone&quot;,&quot;2147483647&quot;,&quot;text&quot;],[&quot;#txtEmail&quot;,&quot;aayush@mail.com&quot;,&quot;text&quot;],[&quot;#rdosex_0,#rdosex_1&quot;,&quot;1&quot;,&quot;radio&quot;]]'; for(a=$.parseJSON(data),b=a.length-1;0&lt;=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>
4

2 回答 2

1

请用

document.getElementsByTagName('head').item(0).appendChild(a);

而不是你使用的

document.body.appendChild(a); 

我希望它会运作良好。

于 2013-05-15T12:47:49.883 回答
1

当您将其作为超链接插入时它会中断的原因是某些字符(尤其是空格'")需要编码。

请参阅JavaScript 小书签是否需要是有效的 URL?了解更多信息。

于 2013-05-15T09:02:05.053 回答