替换 &到&;在网址中
我的页面上有大量 XML Parsing Error: EntityRef: waiting ';'
错误,只是因为开发人员在链接中错误地使用了&符号。
我不要求他使用 &
而不是 &
,而是如何在页面加载后使用 jQuery 替换它们?
编辑:啊,对了,验证器读取源代码,所以它不起作用。还有其他方法可以过滤 HTML 并清除此类愚蠢的错误吗?
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这在 Javascript 中行不通,你确实需要在服务器端解决这个问题。
根据评论,您正在使用 JSP。您可以使用 JSTL (只需删除 /WEB-INF 中的 ="http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar" rel="nofollow noreferrer">jstl-1.2.jar /lib)
和
标签。
默认转义 HTML 实体<
、>
、&
、< code>" 和'
。
可以与 结合使用。 com/products/jsp/jstl/1.1/docs/tlddocs/c/param.html" rel="nofollow noreferrer">
默认情况下对查询参数进行编码。This ain't going to work in Javascript, you really need to solve this at the server side.
As per the comments, you're using JSP. You can use the JSTL (just drop jstl-1.2.jar in
/WEB-INF/lib
)<c:out>
and<c:url>
tags for this.The
<c:out>
by default escapes HTML entities<
,>
,&
,"
and'
. The<c:url>
can be used in combination with<c:param>
which by default encodes query parameters.替换页面的文本内容绝对不会影响您的验证结果,因为验证器不执行脚本。它仍然会看到所获取文档的原始内容。
如果您仍然想出于验证以外的原因执行此操作,您可以尝试类似 此插件< /a>.
Replacing the textual content of a page will have absolutely no effect on your validation results, as a validator does not execute scripts. It will still see the original content of the fetched document.
If you still want to do it for reasons other than validation, you could try something like this plugin.
jQuery 无助于解决验证错误。事实上,验证器甚至不运行 JavaScript。
jQuery will not help solving validation errors. In fact, validators don't even run JavaScript.
页面加载和解析后,您将无法更正解析错误。唯一正确的解决方案是在服务器端进行替换。
You will not be able to correct parse errors after the page has already been loaded and parsed. The only correct solution is to do the replacement on the server side.
即使您可以在 jQuery 中修复它们,到那时页面已经被处理并且仍然会抛出错误。您最好对
&
进行全局搜索并手动替换它们。Even if you could fix them in jQuery, the page is already processed by that point and the errors would still be thrown. You would be better off doing a global search for
&
and manually replacing them.