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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tom
  • 2,604
  • 11
  • 57
  • 96
  • Duplicate of http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript – mwcz Mar 22 '12 at 02:35

2 Answers2

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>
jeremysawesome
  • 7,033
  • 5
  • 33
  • 37
  • 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 Mar 22 '12 at 03:11
  • @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. – AlexMA Mar 22 '12 at 03:22
  • 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. – Tom Mar 22 '12 at 03:36
  • Have you tried the `button` html that I posted in my second edit? – jeremysawesome Mar 22 '12 at 03:39
  • I did, same message except it now says: `startmonitor('7dfa2afbcee33086e1d69cac299c1c5c5efb560b', escape('al's cam'), '0', '2')` – Tom Mar 22 '12 at 03:55
  • Hi @TomPepernic. Unless there is a specific reason you need to encode in JavaScript, just encode the necessary url components with php before outputting to the page. The problem you are running into is exactly what Alex said, your string is being closed. – jeremysawesome Mar 22 '12 at 04:11
  • Ah brilliant, thanks. If your retarded, what does that make me? ;) – Tom Mar 22 '12 at 13:05
2

There are 3 ways to escape special characters:

  • escape()
  • encodeURI()
  • encodeURIComponent()
Joseph
  • 117,725
  • 30
  • 181
  • 234