tags:

views:

119

answers:

3

I am using a bit of JavaScript like this:

echo '<a href="javascript:playSong'."('$row[artist]','$row[title]','$row[sourcefile]')".'">';

My problem is that sometimes my $row[artist] and $row[title] variables contain double qoutes.

When this happens it breaks the javascript:playSong(); function call.

For example if the line was output like this:

<a href="javascript:playSong('Danny Elfman','Beetlejuice Theme (Kamei Halloween Edit)','2009-10-31-10-52-01.4521.data','28330')">

Everything would be fine.

But sometimes the function will look like this:

<a href="javascript:playSong('Danny Elfman','Beetlejuice "Theme" (Kamei Halloween Edit)','2009-10-31-10-52-01.4521.data','28330')">

Which would then cause my site to think the command ends at the double quote before "Theme" and thus cause it to fail.

Is there a way I should be properly quoting my javascript so it treats double quotes inside the function as text and no the end of the function.

I am using addslashes() and have tried various other encodings but nothing like that seems to work.

A: 

Edit: Oh. You need to escape it in html. Try htmlspecialchars:

Mike Blandford
They are but it still fails... Looks ilke javascript:playSong('artist','beetlejuice \ in the link.
ian
+1  A: 

Escaping ONLY with backslash won't help you because you also need to escape them in html, try using &#39; for single quotes and &#34; for double quotes in your embedded js functions.

This should do what you need.

Sinan.

Sinan Y.
Both JS-string-literal-escaping *and* then HTML-escaping are needed when putting code in a string in JavaScript in HTML.
bobince
right also wanted to say that, rephrased my answer.
Sinan Y.
+3  A: 

The best solution here is to stop using href="javascript:… and start using unobtrusive JavaScript and progressive enhancement.

If you do want to continue down this route, then you need to remember that you are dealing with three different languages and generating one from the other in a chain.

Start with the JavaScript. Then make it work with the HTML. Then make it work with the PHP.

javascript:playSong('Danny Elfman','Beetlejuice "Theme" (Kamei Halloween Edit)','2009-10-31-10-52-01.4521.data','28330')

There are no syntax errors here. You just have double quotes in a string.

href="javascript:playSong('Danny Elfman','Beetlejuice "Theme" (Kamei Halloween Edit)','2009-10-31-10-52-01.4521.data','28330')"

Now you hava nested the JavaScript in an HTML attribute which is delimited with double quotes. This means that the double quotes in the JS are now a problem as it terminates the attribute value half way through the script.

Deal with these quotes in the usual way for HTML. Replace them with an HTML entity: &quot;

href="javascript:playSong('Danny Elfman','Beetlejuice &quot;Theme&quot; (Kamei Halloween Edit)','2009-10-31-10-52-01.4521.data','28330')"

Then we get to the PHP:

echo '<a href="javascript:playSong'."('$row[artist]','$row[title]','$row[sourcefile]')".'">';

Dealing with quotes inside quotes inside quotes is a pain. So don't try.

href="javascript:playSong('<?php echo $row[artist] ?>','<?php echo $row[title] ?>','<?php echo $row[sourcefile]; ?>')"

You seem to have lost an argument between examples there.

Follow the normal rules for dealing with inserting content into HTML with PHP: htmlspecialchars

href="javascript:playSong('<?php echo htmlspecialchars($row[artist]); ?>','<?php echo htmlspecialchars($row[title]); ?>','<?php echo htmlspecialchars($row[sourcefile]); ?>')"
David Dorward
totally agree with what you're saying, but in some projects, that you just have to maintain you still need to cope with that kind of problems.
Sinan Y.
Yes if I had known what I was doing I would have made it differently in Jquery but just trying to patch it for now.
ian
This still needs JS-escaping (`addslashes` or `json_encode`) before the `htmlspecialchars` is applied; otherwise apostrophes, backslashes and newlines will cause problems. Note If you have a lot of code like this I'm afraid your app is going to be highly insecure, full of both JS-injections and HTML-injections. I would strongly advise moving to unobtrusive techniques to reduce the amount of nested string escaping you need to do. Also `javascript:` URLs should simply never be used; they are in every way worse than a simple `onclick`.
bobince
I do use addslashes() before anything. I realize it's made pretty crappily but it is a app I only use for myself. I will remake it sometime most likely with jquery.@David Dorward thanks! finally got that working.
ian
Also htmlspecialchars() should have the ENT_QUOTES tag on correct?
ian
You only *need* ENT_QUOTES if you are using single-quotes as the attribute value delimiter, but it's a good practice to always include anyhow.
bobince