This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
When to Use Double or Single Quotes in JavaScript
I think $('#id') and $("#id") both is valid so " and ' character is samething and it is just preference to use ' instead of " ?
Single- and double-quotes do the same thing in JavaScript: they delimit string constants. It's convenient (though a little weird) to have both types of quotes available for the same purpose, because it makes quoting strings with embedded quotes a little easier sometimes. One example: jQuery selectors:
$('input[name="my input"]').val('');
jQuery is just a JavaScript library.
A string quoted with ' characters have have " characters inside it without them being escaped — and vice versa — that is all.
' and " may be used interchangeably in JavaScript/jQuery.
It's a little more robust to use '.
Why? Because it's easier to move JavaScript code between script tags and inline HTML elements - common XHTML syntax is to wrap event code in "" (eg onclick="alert('foo')"). Using single quotes prevents the need to escape (eg "alert(\"foo\")"), and allows you to easily move the code from the inline to an external script page.
Related
We have a FTL that has -
<a href='javascript:func("${event.title}");'>Link</a>
When the value has an apostrophe, like Norman'S Birthday - it breaks.
We used js_string to fix it -
Link
But we also had to change the double quotes around $expression to single quotes - this does Not work with double quotes.
Question -
Is there a way to fix this with the original double quotes around the $expression ?
Something like -
<a href='javascript:func("${event.title?js_string}");'>Link</a>
Note the above does not work.
You need two kind of escaping applied here: JavaScript string escaping and HTML escaping. You need both, as the two formats are independent, and the JavaScript is embedded into HTML.
How to do that... the primitive way is event.title?js_string?html, but it's easy to forget to add that ?html, so don't do that. Instead, use auto-escaping (see https://freemarker.apache.org/docs/dgui_quickstart_template.html#dgui_quickstart_template_autoescaping). If you can't use that form of auto-escaping for some reason (like you are using some old FreeMarker version and aren't allowed to upgrade), put the whole template into <#escape x as x?html>...</#escape>. In both cases, you can just write ${event.title?js_string}, and it will just work.
However, if you are using #escape, ensure that the incompatible_improvements setting (see https://freemarker.apache.org/docs/pgui_config_incompatible_improvements.html) is at least 2.3.20, or else ?html doesn't escape '.
Modify the value of event.title so that single quotes are replaces with ' and double quotes are replaces with ", then you won't have to worry at all about which kind of quotes you use for the rest of it.
?js_string should be outside curly brackets {}. Now you should use double quotes " " around href value, if you wanna handle single quotes ' ' inside the string and vice versa. Single and double quote can not be used in same string, so either of them needs to be replaced to other.
solution:
Link
Same expression can be used in JavaScript to handle special characters
<button type="button" onclick='document.getElementById("demo").innerHTML = "Hello JavaScript!"'>Click to See</button>
When I use the id demo in " " it is showing text properly,
<button onclick="document.getElementById('demo').src='right_1.png'">Right</button>
When I use the id demo in ' ' here then it works
What is difference ?
if you noticed onclick='document.getElementById("demo").innerHTML , just after "onclick=" you are using single quote (') so if you use single quote (') for 'demo' also , JS will interpret it as if you have closed the single quote (') which you have opened at the beginning , hence it will not work. So if you used single quote in the front, you need to use "" in the middle. If you have used double quotes in the front, you need to use single quote in the middle for demo. Hope that clarifies.
Both do work, and which one to use (double-quoted or single-quoted) is up to the programmers preferences.
From my understanding, you can use one quotation-format as literal, and the other one as complementary. Javascript as a scripting language sees a difference between the two quotations, but still makes them binary mean the same thing. This makes the quotations not directly correlate to each other, and hence you don't have to escape the quotes when mixing them.
It is preferable tough that you use the same quotations, and not mix them.
Why and how the Javascript language is built like this, I have no idea. Maybe you should mail ECMA and ask them. I do know tough, that some languages do not see a "difference" between the quotation-format e.g. Python.
What is the difference between split(' ') and the same split(" ").. Is there anything that the second type of split is supported in ie8?
No difference, " " and ' ' are two different way to write the same string literal and the function shouldn't be able to discover which one was used.
Could you reproduce a situation where they would behave differently? Can you post a minimum snippet on jsfiddle?
A difference could occur with nested quotes, e.g. in an onlick assignment. The rule is double quotes may contain single quotes and vice versa.
<button onlick="parts=s.split(' ');">split</button>
or
<button onlick='parts=s.split(" ");'>split</button>
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
single quotes versus double quotes in js
When to Use Double or Single Quotes in JavaScript
What is the difference (if any) between the javascript strings defined below?
var str1 = "Somestring";
var str2 = 'Somestring';
"" and '' mean two very different things to me predominantly writing code in C++ :-)
EDIT: If there is no difference why are there two ways of achieving the same thing and which is considered better practice to use and why. Thanks!
Javascript treats single and double quotes as string delimiters.
If you use single quotes, you can use double quotes inside the string without escaping them.
If you use double quotes, you can use single quotes inside the string without escaping them.
Both examples evaluate to the same thing.
alert(str1 == str2); // true
alert(str1 === str2); // true
Why two ways? Due to the way javascript allows you to mix the two, you can write html attributes out without messy escapes:
var htmlString1 = "<a href='#'>link</a>";
var htmlString2 = 'link';
As for best practice, there is no convention. Use what feels best.
Personally, I like making sure the Javascript I emit matches the HTML (if I double quote attributes, I will delimit JS string with a ', so emitted attributes will use ").
In Javascript a string is a sequence of zero or more Unicode characters enclosed within single or double quotes (' or "). Double-quote characters may be contained within strings delimited by single-quote characters, and single-quote characters may be contained within strings delimited by double quotes.
In client-side JavaScript programming, JavaScript code often contains strings of HTML code, and HTML code often contains strings of JavaScript code. Like JavaScript, HTML uses either single or double quotes to delimit its strings. Thus, when combining JavaScript and HTML, it is a good idea to use one style of quotes for JavaScript and the other style for HTML.
No difference at all.
I believe the answer is there is no difference. They are both strings.
Here would be the usage of '
var mynewhtml = '<body class="myclass" ></body>';
or using "
var mynewhtml = "<body class='myclass' ></body>";
this also works but IMO is harder to read
var mynewhtml = "<body class=\"myclass\" ></body>";
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When to Use Double or Single Quotes in JavaScript
Are there any differences between single and double quotes in javascript?
No, is the right answer most of the time. However if you want to write valid JSON, then you should use double quotes for object literals.
No, except in double quotes you can put single quotes.
e.g. "Don't not do this"
And in single quotes you can put double quotes.
e.g. 'John said "Do this"'
No difference. Just make sure you close the string with whatever you open it with.
Much more sensible to me than other languages (looking at you, C# and PHP...) where single quoted strings are either character literals, or don't expand escaped characters.
One is slightly wider, so you may have a few extra characters disappear to the right (as opposed to the slimmer version) in your favourite IDE.
Seriously though, I always use ', because if I need to quote a HTML element attribute, I always use " and I can't be bothered escaping like so
var html = "hello"
You would mostly use " unless in a position in where you can't escape it. ( It is neater to most developers, Don't ask why )
Also "$myVar" in php will allow the string to have the variables value. ( I know its not javascript, but another example..
In bash,
echo "What is your name?\nMy name is $(whoami)."
will run the function / command whoami.
<button onclick="dosomething(\"test\")">Test</button> Won't work
<button onclick="dosomething("test")">Test</button> Won't work
<section id='"Where-As">
<button onclick="dosomething('test')">Test</button>
<!-- will work -->
</section>
P.S: Valid JSON objects should be using double quotes.
Other fun with different quotes:
console.log('\n\n\n\n'); // Will give you \n\n\n\n as a string.
console.log("\n\n\n\n"); // Will give lines.