473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 3614
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.