468,140 Members | 1,461 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,140 developers. It's quick & easy.

Escaping " and ' characters in Javascript


I am using PHP to generate a little javascript for one of my pages. In
short it allows a user to select a value from a list and pop it into a form
field on a seperate page. This works well unless there is a " or ' in the
character string.

<SCRIPT language=JavaScript>
function Add_Term(SearchTerm) {
window.opener.document.advsearch.name_title.value +=
SearchTerm;
window.close();
}
</SCRIPT>

<a href="javascript:Add_Term('This will work')">"Amarga Presencia"</a>

<a href="javascript:Add_Term('This "won't" work')">"Amarga Presencia"</a>

How do I escape the " and ' characters properly ..? I know this is basic
but I have tried \' and \" and cant think what else to do.
--

NecroJoe
-- US East : KCarpenterRox / Necro / 73
Jul 23 '05 #1
3 3324
I
"NecroJoe" <TA************************@hotmail.com> writes:
<SCRIPT language=JavaScript>
In HTML4, the type attribute is required, and it is always sufficient.
Use:
<script type="text/javascript">
function Add_Term(SearchTerm) {
window.opener.document.advsearch.name_title.value +=
SearchTerm;
window.close();
}
</SCRIPT>

<a href="javascript:Add_Term('This will work')">"Amarga Presencia"</a>
Don't use "javascript:"-URLs. <URL:http://jibbering.com/faq/#FAQ4_24>
<a href="javascript:Add_Term('This "won't" work')">"Amarga Presencia"</a>
No, the HTML attribute is surrounded by "'s, so you can't use them inside
it as well. Use:
---
<a href="javascript:Add_Term('This &quot;won't&quot; work')"> ...
---
(or rather:
---
<a href="needJS.html" onclick="Add_Term('This &quot;won't&quot; work')"> ...
---
)
How do I escape the " and ' characters properly ..?
What you need is the HTML escape, which is &quot; for " and &apos; for '.
If you also need to nest quotes inside the Javascript, then use backslashes
to escape:
----
onclick="foo('ain\'t this &quot;kosher&quot;?');
bar(&quot;No \&quot;dear\&quot;!&quot;);"
---
This is HTML escape for this javascript:
---
foo('ain\'t this "kosher"?');
bar("No \"dear\"!");
---
I know this is basic
but I have tried \' and \" and cant think what else to do.


HTML escapes, not Javascript. :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:br**********@hotpop.com...
Don't use "javascript:"-URLs. <URL:http://jibbering.com/faq/#FAQ4_24>
<a href="javascript:Add_Term('This "won't" work')">"Amarga
Presencia"</a>
No, the HTML attribute is surrounded by "'s, so you can't use them inside
it as well. Use:
---
<a href="javascript:Add_Term('This &quot;won't&quot; work')"> ...
---
(or rather:
---
<a href="needJS.html" onclick="Add_Term('This &quot;won't&quot; work')"> .... ---
)
How do I escape the " and ' characters properly ..?
What you need is the HTML escape, which is &quot; for " and &apos; for '.
If you also need to nest quotes inside the Javascript, then use

backslashes to escape:
----
onclick="foo('ain\'t this &quot;kosher&quot;?');
bar(&quot;No \&quot;dear\&quot;!&quot;);"
---
This is HTML escape for this javascript:
---
foo('ain\'t this "kosher"?');
bar("No \"dear\"!");
---
I know this is basic
but I have tried \' and \" and cant think what else to do.


HTML escapes, not Javascript. :)
/L


I would have snipped a bit more but that is good stuff ;-D -- thanks a lot,
I appreciate it.

--

NecroJoe
-- US East : KCarpenterRox / Necro / 73
Jul 23 '05 #3
Lasse Reichstein Nielsen wrote:
"NecroJoe" <TA************************@hotmail.com> writes:
<a href="javascript:Add_Term('This "won't" work')">"Amarga Presencia"</a>

No, the HTML attribute is surrounded by "'s, so you can't use them inside
it as well. Use:
---
<a href="javascript:Add_Term('This &quot;won't&quot; work')"> ...
---
(or rather:
---
<a href="needJS.html" onclick="Add_Term('This &quot;won't&quot; work')"> ...


Correct, either one won't work ;-)

Use

<a href="javascript:Add_Term('This &quot;won\'t&quot; work')"> ...

or rather

<a href="needJS.html"
onclick="Add_Term('This &quot;won\'t&quot; work')"> ...

instead.
PointedEars
-- does anyone have any info about how can i make an explorer type menu?

Care to be more specific? Windows Explorer? Internet Explorer? Jungle
Explorer? (Fabian in cljs, <br************@ID-174912.news.uni-berlin.de>)
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Lisa | last post: by
4 posts views Thread by sankofa | last post: by
1 post views Thread by Ted Weatherly | last post: by
5 posts views Thread by Lucian Sandor | last post: by
4 posts views Thread by James Black | last post: by
3 posts views Thread by Arthur Dent | last post: by
3 posts views Thread by Taras_96 | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.