473,396 Members | 1,864 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.

Function to make strings javascript friendly

I want to put some dynamic text into some href's on my form:

<a href="<url>" onMouseOver="window.status='<dynamic text>'; return true"
onMouseOut="window.status=''; return true>

The dynamic text will from time to time have single quotes or double quotes
in them - so before writing a function to make these dynamic strings work -
wanted to check to see if such a function exist (that will handle any
troublesome characters).

Any insight/pointers you could provide - much appreciated.

BBB
Jul 23 '05 #1
8 3408
Billy Boone wrote:
I want to put some dynamic text into some href's on my form:

<a href="<url>" onMouseOver="window.status='<dynamic text>'; return true"
onMouseOut="window.status=''; return true>

The dynamic text will from time to time have single quotes or double quotes
in them - so before writing a function to make these dynamic strings work -
wanted to check to see if such a function exist (that will handle any
troublesome characters).

Any insight/pointers you could provide - much appreciated.


You may try escape() and unescape()
Or for newer UAs encodeURIComponent() and decodeURIComponent()

Daniel
Jul 23 '05 #2
escape won't work (at least from what I've read) - need the text to be human
readable
I Am Here

Not:

I%20Am%20Here.
"Daniel Kirsch" <Iw*****************@gmx.de> wrote in message
news:d8*************@news.t-online.com...
Billy Boone wrote:
I want to put some dynamic text into some href's on my form:

<a href="<url>" onMouseOver="window.status='<dynamic text>'; return true" onMouseOut="window.status=''; return true>

The dynamic text will from time to time have single quotes or double quotes in them - so before writing a function to make these dynamic strings work - wanted to check to see if such a function exist (that will handle any
troublesome characters).

Any insight/pointers you could provide - much appreciated.


You may try escape() and unescape()
Or for newer UAs encodeURIComponent() and decodeURIComponent()

Daniel

Jul 23 '05 #3
Billy Boone wrote:
escape won't work (at least from what I've read) - need the text to be human
readable


Well, if your string is allready a JavaScript string, it should work
without any further changes to the string.

If not, you may show us a concrete example of what you have, what you
get and what you want.

Daniel
Jul 23 '05 #4
Billy Boone wrote:
I want to put some dynamic text into some href's on my form:
<a href="<url>" onMouseOver="window.status='<dynamic text>'; return
true" onMouseOut="window.status=''; return true>
The dynamic text will from time to time have single quotes or double
quotes in them - so before writing a function to make these dynamic
strings work - wanted to check to see if such a function exist (that
will handle any troublesome characters).


You need to do this on the server-side.

Do your normal HTML-escaping (if the code will be within an HTML tag as
above) then escape the following:
' to \'
" to \"
(newline) to \n

That should cover it, and the text will then be safe enough to put within
your quoted string.

I did this in Struts, for example, by making my own <bean:write> tag which
took another argument of javascriptEscape="true" to do these conversions.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #5
I'm getting info from a database:

So I may have a string:

Today's date is 6/8/2005

So the single quote will mess things up. Or the string could come with a
double quote in it.

"Daniel Kirsch" <Iw*****************@gmx.de> wrote in message
news:d8*************@news.t-online.com...
Billy Boone wrote:
escape won't work (at least from what I've read) - need the text to be human readable


Well, if your string is allready a JavaScript string, it should work
without any further changes to the string.

If not, you may show us a concrete example of what you have, what you
get and what you want.

Daniel

Jul 23 '05 #6


Billy Boone wrote:
I want to put some dynamic text into some href's on my form:

<a href="<url>" onMouseOver="window.status='<dynamic text>'; return true"
onMouseOut="window.status=''; return true>

The dynamic text will from time to time have single quotes or double quotes
in them - so before writing a function to make these dynamic strings work -
wanted to check to see if such a function exist (that will handle any
troublesome characters).


If you are generating the dynamic text on the server then it is a server
side problem where the solution depends on the scripting language used
on the server.
Are you using JavaScript on the server? Otherwise ask in a group about
the server side language of your choice.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #7
ASM
Billy Boone wrote:
I want to put some dynamic text into some href's on my form:
don't know what is "dynanic" text
<a href="<url>" onMouseOver="window.status='<dynamic text>'; return true"
onMouseOut="window.status=''; return true>


onMouseOut="window.status=''; return true;">

and p e ?
onMouseOver="window.status=<dynamic text>; return true;"
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #8
fox


Billy Boone wrote:
I want to put some dynamic text into some href's on my form:

<a href="<url>" onMouseOver="window.status='<dynamic text>'; return true"
onMouseOut="window.status=''; return true>

The dynamic text will from time to time have single quotes or double quotes
in them - so before writing a function to make these dynamic strings work -
wanted to check to see if such a function exist (that will handle any
troublesome characters).

Any insight/pointers you could provide - much appreciated.

BBB

String.prototype.addslashes = function()
{
return this.replace(/([\'\"])/g,"\\$1");
}
Jul 23 '05 #9

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

Similar topics

39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
9
by: Max | last post by:
I'm new with Javascript and can't seem to figure out what I'm doing wrong here as I'm not able to pass a simple variable to a function. In the head of doc I have: <script...
8
by: rdavis7408 | last post by:
I am attempting what I would think would be a simple calculation of the cost of traveling a single mile. But I can not figure this out. The following is my script. Any help would be appreciated. ...
1
by: vocalise | last post by:
The title probably isn't very clear, but I haven't been able to find this problem (or I must be having problems figuring out which search strings to use) so I apologize if this has been addressed...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript....
1
by: wkerplunk | last post by:
Below is what I have build with several different languages. It works great but I need help, I am stuck. When you click on an item in the dropdown autocomplete div it does a mousedown function...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
4
by: barcaroller | last post by:
I was thinking of buying Scott Meyer's second book (More Effective C++) and noticed that it has not been updated since 1995 (unlike his other two famous books). Does anyone know (rumour or...
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
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?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.