473,657 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A tricky Javascript question...

Hi,

I have one very simple tricky question which is quite interesting, I
would like to share with all of you here...
//=============== =============== =========

<script type="text/javascript">

function func1() {
var str = "123\'s";
document.write( '<a href="javascrip t:alert(\'' + str+'\')">test
link</a>');
}

</script>

//=============== =============== =========

If "str" is just a normal text, e.g. "123", then you can have a link
which lead to an alert box when onclick.

However, when there is a quote in the string, the function will not
work...

Any workaround for this problem?
Thanks.

Rdgs,
Howa

Feb 22 '06 #1
9 1717
Hi,
However, when there is a quote in the string, the function will not
work...


Try to double escape your string:

var str = "123\\''s";

HTH,

Llorenc
Feb 22 '06 #2
I agree this is ok, but what if the string is pass from a variable?

e.g.

func1("123\'s") ;
function func1(str) {
document.write( '<a href="javascrip t:alert(\'' + str+'\')">test
link</a>');
}

Feb 22 '06 #3
wrote on 22 feb 2006 in comp.lang.javas cript:
agree this is ok,


What is ok?

Please quote what you are replying to. If you want to post a followup via
groups.google.c om, don't use the "Reply" link at the bottom of the article.
Click on "show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
<http://www.safalra.com/special/googlegroupsrep ly/>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 22 '06 #4
On 22/02/2006 14:40, ho******@gmail. com wrote:

[snip]
function func1() {
var str = "123\'s";
The single quote doesn't need to be escaped there. The literal, "123's"
is exactly equivalent.
document.write( '<a href="javascrip t:alert(\'' + str+'\')">test
link</a>');
As this code is within HTML, the closing tag needs to broken up. That
is, instead of '</a>', use '<\/a>'. In addition, using the javascript:
pseudo-scheme is inadvisable due to its side effects, even in a
situation like this where the anchor will only exist if scripting is
enabled.

[snip]
However, when there is a quote in the string, the function will not
work...

Any workaround for this problem?


Yes, a simple one. You need to observe what the output will be for this
code. For the example above:

<a href="javascrip t:alert('123's' )">test link</a>

The solution is to include a literal backslash in the string, str. This
can either be added manually:

var str = "123\\'s";

document.write( '<a href="#" onclick="alert( \''
+ str + '\');return false;">test link<\/a>');

or programatically :

var str = '123\'s';

document.write( '<a href="#" onclick="alert( \''
+ str.replace(/'/g, '\\\'')
+ '\');return false;">test link<\/a>');

In either case, the output will now be:

<a href="#" onclick="alert( '123\'s');retur n false;">test link</a>

Hope that helps,
Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Feb 22 '06 #5
Hi,
I agree this is ok, but what if the string is pass from a variable?


Try this:

<script type="text/javascript">
var str = '123\'s';
function func1(str) {
document.write( '<a href="javascrip t:alert(\'' + str.replace('\' ',
'\\\'') + '\')">test link</a>');
}
func1(str);
</script>

HTH,

Llorenc
Feb 22 '06 #6
Sorry for that.

Thanks for your advice.
Evertjan. wrote:
wrote on 22 feb 2006 in comp.lang.javas cript:
agree this is ok,


What is ok?

Please quote what you are replying to. If you want to post a followup via
groups.google.c om, don't use the "Reply" link at the bottom of the article.
Click on "show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
<http://www.safalra.com/special/googlegroupsrep ly/>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Feb 22 '06 #7
Michael Winter wrote:
On 22/02/2006 14:40, ho******@gmail. com wrote:
[...]
The solution is to include a literal backslash in the string, str. This
can either be added manually:

var str = "123\\'s";

document.write( '<a href="#" onclick="alert( \''
+ str + '\');return false;">test link<\/a>');

or programatically :

var str = '123\'s';

document.write( '<a href="#" onclick="alert( \''
+ str.replace(/'/g, '\\\'')
+ '\');return false;">test link<\/a>');

In either case, the output will now be:

<a href="#" onclick="alert( '123\'s');retur n false;">test link</a>

That's one solution (probably the best). Another is to use the Unicode
hex code:

<a href="#" onclick="alert( '123\u0027s');r eturn false;">test link</a>

--
Rob
Feb 23 '06 #8
RobG wrote:
Michael Winter wrote:
On 22/02/2006 14:40, ho******@gmail. com wrote:
[...]
In either case, the output will now be:

<a href="#" onclick="alert( '123\'s');retur n false;">test link</a>


That's one solution (probably the best). Another is to use the Unicode
hex code:

<a href="#" onclick="alert( '123\u0027s');r eturn false;">test link</a>


Or the HTML UCS character reference:

<... onclick="alert( '123&#x27;s');r eturn false;">...</a>

<... onclick="alert( '123's');return false;">...</a>
PointedEars
Feb 24 '06 #9
On 22/02/2006 23:54, RobG wrote:
Michael Winter wrote:
[snip]
The solution is to include a literal backslash in the string, str.
This can either be added manually:

var str = "123\\'s";


[snip]
Another is to use the Unicode hex code:

<a href="#" onclick="alert( '123\u0027s');r eturn false;">test link</a>


That would still need to be subject to an escaping mechanism. That is,
the variable above (str) would need to be assigned '123\\u0027s'.

Still, I should have written 'A solution' as I did also consider
character references, as Thomas mentioned.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Feb 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
13770
by: JZ | last post by:
Oracle 9iR2 I have a table: SQL> select * from test; A B C ------------------- ---------- ---------- 01/01/2004 10:00:00 1 1 01/01/2004 11:00:00 1 2
1
1697
by: moon | last post by:
Wondering if this is possible? Situation: I have a frameset with a few frames in it that loads side graphics, main navigation, subnavigation and content. Everything works fine. Now, I decided to add a gateway index page with buttons that lead to a specific page within the frameset. ie. aboutus, contact, products,etc... The buttons work but it obviously does not load the whole frameset with the specific page in it. It just loads the page.
1
1989
by: Tim | last post by:
Hello, I'm extremely puzzled; I cannot figure out what I'm doing wrong. Here's the situation. I would really appreciate any suggestions. I'm modifying a shopping cart in the following way. I've just added new prices to several drop-downs on our javascript-based ordering page. These new prices must have shipping costs added to them at the bottom of the ordering system in a text box called "Shipping", while the existing prices do not...
3
2292
by: Colin Steadman | last post by:
Help! I'm a stupid HTML bod and I dont do Javascript! I have a grey coloured table that displays certain columns in either red, green or orange to give meaning and emphasis to certain data. What I want to do now is setup some kind of javascript event so that when the user mouse's over a row the row changes colour to highlight it. I've discovered however that I can only change the row into one specific colour, and then back again into...
3
1838
by: Andy | last post by:
Hi, I am complete JavaScript novice and would really appreciate some help with this code: ===================================================================== <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%> <!--#include file="Connections/ssdb.asp" -->
25
3394
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
8
1879
by: pras.vaidya | last post by:
Hi , below given question was asked to me during an interview and i figured it out little tricky . It would be a great help if anyone could solve it. Code : - main() { char *s1="abcd",*s2=NULL; /* From here you call a function copy which has return type void .
6
1478
by: localhost | last post by:
I have a string that looks like this: "document.form1.textBox1.focus ();document.form1.textBox1.select();" I want to replace the text between "document.form1." and ".focus()", as well as the text betwen "document.form1." and ".select ()".
2
311
by: Roy | last post by:
Ok, what I'd like to do is have a datagrid which, when someone moved their mouse cursor over the cells, more information would pop up (using JavaScript) about that specific cell, depending on it's value. In other words, an onmouseOver for each individual cell with popup info specific to it's value. I'm curious if this is even possible given that a datagrid is server side and javascript runs client side...
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8602
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.