Pal*_*ini 8 javascript html-entities html-escape-characters
在你提问之前,我已经在Stack Overflow中搜索了很多,并且我谷歌了几千次.我见过的任何其他案例都帮助了我.
我们来看看我的问题:
我正在尝试在我的代码中使用以下脚本:
<script type='text/javaScript'>
document.write("<script src='/feeds/posts/default/-/"+hom_cat1+"?max-results=1&orderby=published&alt=json-in-script&callback=showhomeposts1'></script>");
</script>
Run Code Online (Sandbox Code Playgroud)
但我正在使用Blogger并且它无法正确检测我的代码(请注意红色脚本结束标记):

有了这个,我无法保存模板.所以我试图使用这个网站将我的代码转换成HTML实体.当我编码时,将其放入我的模板并保存,我得到:
Uncaught SyntaxError: Unexpected token ILLEGAL
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试使用的编码字符串:
<script type='text/javaScript'>document.write("<script src='/feeds/posts/default/-/"+hom_cat1+"?max-results=1&orderby=published&alt=json-in-script&callback=showhomeposts1'></script>");</script>
Run Code Online (Sandbox Code Playgroud)
Dag*_*bit 22
问题是传递给的字符串document.write包含字符</script>,最终会过早地终止document.write调用的脚本元素.
字符</script>不能出现在脚本中的任何位置,因为HTML解析器无法将其与实际</script>标记区分开来.
您可以尝试这样的事情:
document.write("<script src='...'></scr" + "ipt>");
Run Code Online (Sandbox Code Playgroud)
或者,如评论中所述:
document.write("<script src='...'><\/script>");
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用DOM API创建script元素并将其插入到文档中.这里的其他答案给出了一些建议,但是实现存在潜在的问题(例如,document.body.appendChild如果你尝试从内部调用它会抛出一个TypeError head).像这样的东西会更强大:
(function() {
var s = document.getElementsByTagName('script')[0];
var script = document.createElement('script');
script.src = 'http://something.com';
s.parentNode.insertBefore(script, s);
}());
Run Code Online (Sandbox Code Playgroud)
另外,type='text/javaScript'是不正确的; 使用text/javascript或省略type属性.
| 归档时间: |
|
| 查看次数: |
8253 次 |
| 最近记录: |