I have used below syntzx to replace single quote with double single quote issue
str.replace(/'/g,"''");
but it's replace everytime when it load page. like
I have text
" Test's and test's page and test's event"
then first time ,it will be
" Test''s and test''s page and test''s event"
then again
" Test'''s and test'''s page and test'''s event"
then next loading
" Test''''s and test''''s page and test''''s event"
can you please help to get just single to double single quote only?
If it's safe to assume that there won't be three or more quotes in a row, try this:
str.replace(/'+/g,"''")
If the assumption is not safe, and you just want to replace "a quote by itself" with two quotes, leaving multi-quotes alone, try this:
str.replace(/''?('*)/g,"''$1");
That being said, you might want to look into why it's replacing more than once in the first place ;)
Related
So I have to make a button that changes an image that I have (which I then have to be able to change back to the previous image with a click of the same button). However the image when it changes doesn't show.
Here's the code, if it's not well put in keep in mind this is my first using this site.
//FUNCTIONS
function changerPingouin(pingouin){
document.getElementById("pingouin").src='C:\Users\name\Desktop\images1/pingouin_rouge_grand.png';
}
<body>
<img src='C:\Users\name\Desktop\images1/pingouin_grand.png' id="pingouin">
<img src='C:\Users\name\Desktop\images1/pingouin_rouge_grand.png' id="pingouin_rouge">
<button onclick="changerPingouin();">
Changer de Pingouin
</button>
The first image shows and I have other functions that work with it. However when I click the button, it doesn't display the other image. For this reason I haven't started the part where another click of the button changes the image back to the first one "pingouin_grand".Thanks.
Error:
/C:/UsersnameDesktopimages1/pingouin_rouge_grand.png:1 GET file:///C:/UsersnameDesktopimages1/pingouin_rouge_grand.png net::ERR_FILE_NOT_FOUND Image (async) changerPingouin # Module_8PLs.html:21 onclick # Module_8PLs.html:89
From the error, the file is not found because the path read is C:/UsersnameDesktopimages1/pingouin_rouge_grand.png. There are no backslash separating the directories.
The backslash needs to be escaped. In JavaScript, the backslash is used to escape special characters, such as newlines (\n). If you want to use a literal backslash, a double backslash has to be used. Try changing the image source to 'C:\\Users\\name\\Desktop\\images1\\pingouin_rouge_grand.png'. Do the same to the first image.
Description:
I have a very simple button that I want to call a javascript function when clicked. Everything works fine with the function and the button with the exception of one issue that I can't seem to overcome.
The Javascript function copies a string in to the clipboard to be easily pasted for the user. The string is pulled in from another area that is free text for the user to put in what ever they want. However, if the user adds a double quote, it escapes the parent string.
Example:
Example of working code button:
<button id="Demo" onclick="copytoclipboard('some text to copy goes here')">Copy to Clipboard</button>
Example of when the button will not work:
<button id="Demo" onclick="copytoclipboard('some "text" to copy goes here')">Copy to Clipboard</button>
As you may be able to see, the first double quotation mark is ending the parent string.
What I have tried:
So far I have tried escaping the double quote but this doesn't appear to work due to the fact that it is a child string.
Example:
<button id="Demo" onclick="copytoclipboard('some \"text\" to copy goes here')">Copy to Clipboard</button>
Back to the question:
How do I prevent any double quotes in the child string the ending the parent string that is in turn preventing my onclick function to run?
This breaks down to: you're looking to escape data (doesn't matter whether plain text or javascript) in an XML (or more specifically HTML) attribute value.
You'll find plenty of info about that - in this case the easiest way is to escape the doublequote char (") via "
<button onclick="alert("foobar");">Click me</button>
The way that you tried escaping the double quote was almost perfect but you need to do it like this:
<button id="Demo" onclick='copytoclipboard("some \"text\" to copy goes here")'>Copy to Clipboard</button>
You can learn more about escaping special characters in here.
I am trying to turn a <div> into a link to local HTML document (./lilo/index.html) using JavaScript.
HTML
<div class="pagelist_item" onClick="goto("./lilo")">
<h4>Test Button</h4>
<h6>Discription</h6>
</div>
JavaScript
function goto(url){
window.location = url;
alert(url);
}
See http://jsfiddle.net/6HHTd/
But when I click the button, nothing happens.
Why does this not work?
Your quotes are incorrect in this line:
<div class="pagelist_item" onClick="goto("./lilo")">
jsfiddle even shows the error in red text.
Using apostrophes makes it easier to fix:
<div class="pagelist_item" onClick="goto('./lilo')">
To clarify, in "hi "there" you" the second double-quote matches with the first, closing the string and causing an error with the rest of the expression. Escaping the quotes with back-slashes works "hi \"there\" you" but embedding apostrophes (single-quotes) within double-quotes is often easier. (JavaScript is happy to use either single or double-quotes to delimit strings.)
Also rename your function from goto, as it is a reserved keyword.
Use jquery as follows
$('.pagelist_item').click(function(){
window.location="./lilo";
});
Fiddle
I am trying to put some text along with quotes in a DIV like this
"All is well that ends well"
now the text is dynamically generated and I am using javascript font replacement plugin (CUFON) for quotes around the text, sometime ending quote drops down to next line because of word wrapping like this
"All is well that ends well
"
How can I prevent that? I couldn't find a way to prevent it. code looks something like that
$("#q1").html('<span class="blue_quotes">“ </span>');
$("#ct1").html($.trim(commentText)+'<span class="blue_quotes">” </span>');
I added span tags for quotes because I need different font and color for quotes
You can use escape sequence \ for such cases ..
But I prefer using unicodes for such cases..
If you want to use the first approach try this
$("#q1").html('<span class="blue_quotes"> \“ </span>');
Check FIDDLE
I am not finding a solution on this one using JavaScript (to utilize localStorage) in a JSP.
Trying to pass something with apostrophe. I have done a .replaceAll() and replaced the ' with ' and it still passes it as an '.
I have also tried a .split("'") and replaced the apostrophe with:
(\' , ' , \', '' , ''' and '\'')
All of these just pass an apostrophe to the function (what I see when I hover over the link) like this:
Save job
With a and b being the two split substrings but with no effect. I do notice that spaces are converted into %20, but that's little comfort. Any other ideas?
Your JSP code is irrelevant. Decide what HTML you want to produce and produce it.
The following are all valid HTML markup:
<a href="saveJob('Bob\'s Question')"> …
<a href="saveJob("Bob's Question")"> …
<a href="saveJob('He said "Go Away"')"> …
<a href='saveJob("He said \"Go Away\"")"> …
… and the following are invalid:
<a href="saveJob('Bob's Question')"> <!-- JS string ends early -->
<a href="saveJob("Bob's Question")"> <!-- HTML attribute ends early -->
<a href="saveJob('Bob's Question')"> <!-- JS string ends early -->
<a href="saveJob('He said "Go Away"')"> <!-- HTML attribute ends early -->
You cannot use your HTML attribute delimiter in your attribute value except as an HTML entity. You cannot use your JavaScript string delimiter in your JavaScript string unless you escape it, even if you use an HTML entity to describe it.
In general, you should not be putting JavaScript in your HTML (you should attach event handlers to your markup programmatically, from script), and you especially shouldn't be abusing an HTML anchor as a JavaScript trigger (either use an HTML anchor to a valid URL and let JavaScript hijack the link if enabled, or use a <button> or other element to invoke script-only side effects).
As you've noticed, such manual string escape tasks can be quite tricky; covering apostrophes won't even get you all the way: what if there's a newline in the string? That would break the script as well.
I would recommend converting your data to a JSON object, perhaps using JSON-taglib. This should take care of all required escaping for you.
The Phrogz solution
<a href="saveJob("Bob's Question")">
works fine if you have only apostrophes in your text.
If your text contains both apostrophes and quotes, you can use a hidden div (div with style='display:none;') for the text, pass the id of the div to saveJob instead of passing the text itself, and get the text inside saveJob by using
document.getElementById(myId).innerHTML