0

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:

<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. Such as Al's camera. Because of the special character it messes up the window.open line. Anyone have ideas how I can rewrite the window.open line to accommodate this?

1

2 Answers 2

2

You might try 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.

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. 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. Just encode with php's urlencode function before outputting to the page.

<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>
Sign up to request clarification or add additional context in comments.

7 Comments

Still can't seem to get it right no matter what I try. Firebug gives this error: missing ) after argument list startmonitor('d17ddbf8795d7b7138976caa7d7456eeb957e38e', 'al's cam', '0', '2');. It points to the column right after the al's
@Tom Pepernic 'al's cam' has got you there. The apostrophe in al's is closing your string. Try 'al\'s cam'. An escape character won't be necessary if you're getting the value from an input, which I assume you are.
Not sure I follow. User input created this camera name. But that value is shown to the user when they edit it as well. So not sure I can escape the character.
Have you tried the button html that I posted in my second edit?
I did, same message except it now says: startmonitor('7dfa2afbcee33086e1d69cac299c1c5c5efb560b', escape('al's cam'), '0', '2')
|
2

There are 3 ways to escape special characters:

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

1 Comment

And the opposite are unescape(), decodeURI(), and decodeURIComponenet().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.