472,954 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

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 3579
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lisa | last post by:
I need to apply the HTML formatting tags and the French accented characters in a XML document. The XML is generated from a database that has HTML tags and French accented characters in the records....
4
by: sankofa | last post by:
hi, i can't seem to be able to escape my single quote properly... is it even possible in javascript? this is a portion of my code.. var DLEWIS="Pastor Lewis"; .... Sermon is a yser-defined...
1
by: Ted Weatherly | last post by:
Hello, I want to dynamically create a table cell with a textfield in it. The value for the textfield can have quotes. e.g. I have this snippet of javascript code: var td =...
5
by: Lucian Sandor | last post by:
Hello everyone, While I'm a newbie here, I a not new to google, so please don't send me back, it would be useless. First of all I have to specify I am working on a Blogger.com template, therefore...
4
by: James Black | last post by:
I have an application that relies on JSON, and I realized yesterday that I when the user types at least some of these characters (most likely all, haven't had time to verify) that the parser has...
4
by: Jon | last post by:
Hi, I used XslCompiledTransform with the following Xsl file. The <xsl:text disable-output-escaping="yes"does not work when using XslCompiledTransform to do the trnasform (namely the output...
3
by: Arthur Dent | last post by:
Hello All... I am in an app that needs to write out an XML document for transmittal to an outside organization. All good and fine... I create the XmlDocument object, append all my nodes, and...
3
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only...
1
by: David Henderson | last post by:
I know 'disable-output-escaping' has been discussed in the past, but I can't put my finger on any of the threads to see if my current problem is addressed. Sorry for re-asking the question if it...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.