473,396 Members | 1,816 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,396 software developers and data experts.

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="javascript: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 1708
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="javascript:alert(\'' + str+'\')">test
link</a>');
}

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


What is ok?

Please quote what you are replying to. If you want to post a followup via
groups.google.com, 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/googlegroupsreply/>

--
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="javascript: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="javascript: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');return 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="javascript: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.javascript:
agree this is ok,


What is ok?

Please quote what you are replying to. If you want to post a followup via
groups.google.com, 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/googlegroupsreply/>

--
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');return 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');return 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');return 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');return false;">test link</a>


Or the HTML UCS character reference:

<... onclick="alert('123&#x27;s');return 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');return 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
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...
1
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...
1
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....
3
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. ...
3
by: Andy | last post by:
Hi, I am complete JavaScript novice and would really appreciate some help with this code: ===================================================================== <%@LANGUAGE="VBSCRIPT"...
25
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
8
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...
6
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...
2
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...
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,...

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.