简体   繁体   English

用javascript转义字符

[英]escape characters with javascript

I have a function that opens up a browser window: 我有一个打开浏览器窗口的功能:

function startmonitor(camerahash, cameraname, cameraflashquality, cameramotion)
{
    window.open("mydir/file.php?user="+'<?php echo $id_hash; ?>'+"&camera="+camerahash+"&name="+cameraname+"&quality="+cameraflashquality+"&motion="+cameramotion, "Webcam_monitor");
}

cameraname is passed in from a button in html: cameraname是通过html中的按钮传递的:

<button id="monitor" onclick="startmonitor('<?php echo $result_cameras[$i]["camera_hash"]; ?>', '<?php echo $result_cameras[$i]["camera_name"]; ?>', '<?php echo $camera_quality_flash; ?>', '<?php echo $camera_motion; ?>');">Start Camera</button>

cameraname does take special characters. cameraname确实带有特殊字符。 Such as Al's camera . Al's camera Because of the special character it messes up the window.open line. 由于特殊字符,它弄乱了window.open行。 Anyone have ideas how I can rewrite the window.open line to accommodate this? 任何人都有想法如何重写window.open行以适应这一点?

You might try encodeUri 您可以尝试encodeUri

function startmonitor(camerahash, cameraname, cameraflashquality, cameramotion)
{
    window.open(encodeUri("file.php?user="+'<?php echo $id_hash; ?>'+"&camera="+camerahash+"&name="+cameraname+"&quality="+cameraflashquality+"&motion="+cameramotion), "Webcam_monitor");
}

Edit: Actually, because you are wanting to encode the apostrophe, you would need to use the escape function around the variables you wish to escape. 编辑:实际上,由于您要对单引号进行编码,因此需要在要转义的变量周围使用转义功能。 In this case it would be used around the cameraname variable. 在这种情况下,它将在cameraname变量周围使用。

function startmonitor(camerahash, cameraname, cameraflashquality, cameramotion)
{
    window.open("file.php?user="+'<?php echo $id_hash; ?>'+"&camera="+camerahash+"&name="+escape(cameraname)+"&quality="+cameraflashquality+"&motion="+cameramotion, "Webcam_monitor");
}

Edit 2 OK, it looks like, based on your comment below, that you need to escape cameraname before it enters your startmonitor function. 编辑2 OK,根据下面的评论,您cameraname需要在进入startmonitor函数之前对cameraname进行转义。 So you would actually escape it in your button code and not in your function code. 因此,您实际上将在按钮代码而不是功能代码中对其进行转义。

<button id="monitor" onclick="startmonitor('<?php echo $result_cameras[$i]["camera_hash"]; ?>', escape('<?php echo $result_cameras[$i]["camera_name"]; ?>'), '<?php echo $camera_quality_flash; ?>', '<?php echo $camera_motion; ?>');">Start Camera</button>

Edit 3 Wow - I'm retarded. 编辑3哇-我很智障。 Just encode with php's urlencode function before outputting to the page. 只需在输出到页面之前使用php的urlencode函数进行编码即可。

<button id="monitor" onclick="startmonitor('<?php echo $result_cameras[$i]["camera_hash"]; ?>', <?php echo urlencode($result_cameras[$i]["camera_name"]); ?>, '<?php echo $camera_quality_flash; ?>', '<?php echo $camera_motion; ?>');">Start Camera</button>

有3种转义特殊字符的方法:

  • escape()
  • encodeURI()
  • encodeURIComponent()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM