我如何逃脱的符号在JavaScript字符串,这样的页面将验证严格? [英] How do I escape an ampersand in a javascript string so that the page will validate strict?

查看:108
本文介绍了我如何逃脱的符号在JavaScript字符串,这样的页面将验证严格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个dataString传递到一个Ajax调用使用JQuery。在通话中,我们构建了GET参数,然后将它们发送到PHP页面在接收端。麻烦的是,该数据串有&符号在他们和HTML严格的验证器弄僵就可以了。

I am trying to pass a dataString to to an ajax call using JQuery. In the call, I construct the get parameters and then send them to the php page on the receiving end. The trouble is that the data string has ampersands in them and the HTML strict validator is chocking on it.

下面是code:

$(document).ready(function(){
	$("input#email").focus();
	$('#login_submit').submit(function(){
		var username = $('input#email').val();
		var password = $('input#password').val();
		var remember = $('input#remember').attr("checked");
		var dataString = "email="+username+"&password="+password+"&remember="+remember;
		$.post('login.php', dataString, function(data) {
			if (data == 'Login Succeeded.') {
				location.reload(true);
			} else {
				$("input#email").focus();
				$("#login_msg").html(data).effect("pulsate", {times: 2}, 1000);	
			}
		});			
		return false;
	});
});

和这里是验证消息的一个例子:不能生成一般实体密码系统标识符

and here is an example of the validator message: cannot generate system identifier for general entity "password".

var dataString = "email="+username+"&password="+password+"&remember="+rememb…

(在验证了P的第一个符号后标记为红色表示故障点)。

(in the validator the "p" after the first ampersand is marked red indicating the point of the failure).

推荐答案

尝试把你的JavaScript的一个CDATA块中是这样的:

Try putting your javascript inside a CDATA block like this:

<script type="text/javascript">
<![CDATA[
// content of your Javascript goes here
]]>
</script>

这应该让它通过验证。为了更加安全,你可以添加JavaScript注释的CDATA标记左右,从旧的浏览器谁不明白的CDATA标签隐藏起来:

which should make it pass validation. To be extra safe you can add Javascript comments around the CDATA tags to hide them from older browsers who don't understand the CDATA tag:

<script type="text/javascript">
/* <![CDATA[ */
// content of your Javascript goes here
/* ]]> */
</script>

这篇关于我如何逃脱的符号在JavaScript字符串,这样的页面将验证严格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆