Backslash does not escape double quotes - javascript

The following string shows double quotes and backslsh when runs:
"<img src=\"abc/xyz/"
What am I doing wrong?
Thanks

Not sure where you are using it, but a simple solution use single quotes on outside and remove backslash.
'<img src="abc/xyz" />'

If this is your full string, it isn't complete. There's no closing quote " nor > bracket
This would be a valid <img> element string
"<img src=\"abc/xyz/\">"

Related

How to set up a javascript variable with apostrophes and quotations marks (Can't escape it!)

I am currently working on a project where I don't have an access to their server side. All I can do is retrieve the blog data from their template editor, but it seems like there's no way to escape HTML using their template syntax. This is bad, but I need to assign the HTML in a javascript variable to create a Json data.
In Python, you'd use both apostrophes and quotation marks without escaping it by using three apostrophes as such:
example = '''
this '/ is great! ""Wahjhahaha
'''
Is there any equivalent way of this in Javascript? As I mentioned, I can't escape the HTML data.
"\" is the escape character in javascript.
var example = "This is a string with \' \" ";
console.log(example);
There are several approches depending on what you want to do.
You can use single quoute inside double quote.
You can use double quote inside single quote.
And if you want to use double quote inside double quote you have to use backslash (\) before the element. The same applies for single quote
console.log('This is double quote inside single quote "');
console.log("This is single quote inside double quote '");
console.log('This is single quote inside single quote \'');
console.log("This is double quote inside double quote \"");
console.log('\' " " \'');
console.log("' \" \" '");
EDIT
You need to use function replace()
var str = "Using single quotes ' ' sdsd 'sd 'dssd sdf' 'sdf'";
str = str.replace(/'/g, "\\'");
console.log(str);
Similarly you can do with double quotes.
Alternatively you can use backticks, but I would not recommend it (backticks in IE11)
console.log(`Here you can use both ' single and " double quotes without backslashes`);

How do I show single and double quotes in a text alert in javascript?

I want a javascript alert that reads, "It's "Hammer" time!"
What is the best code to write this?
Although you could use a string with ' and escape the ', or a string with " and escape the "s, it would be better to use a template literal, which doesn't require escaping of quotes because its delimiter is the backtick:
alert(`"It's "Hammer" time!"`);
For displaying single or double quotes, you can write your code like this alert("\"It's \"Hammer\" time!\"")
You need to escape it using \
For example:
alert("Something \"here\"!!");
Escape the "s other than the ones from the begining and ending..
alert("\"It's \"Hammer\" time!\"")
How to display double quotes in JavaScript
Or use string interpolation, where you can have both without escaping.
alert(`Hello, I'm "nobody"!`);

escape the characters php with javascript inside,correct syntax quotes

how should be the correct syntax of quotes for this piece of code ?
<?php
$cod="<div id='id1' onclick='xmlhttpPost(index.php?ajax="'this.id'") class='divazienda'>".$row[0];
?>
my problem is that I do not take the result of this.id of the js
Try this:
$cod="<div id=\"id1\" onclick=\"xmlhttpPost('index.php?ajax=' + this.id);\" class=\"divazienda\">".$row[0];
Escape double quotes (for each of the div attributes) as they are within the php double quotes
As you are passing a string with the current element id into the onclick function - the string part needs to be in quotes and the variable part needs to be out of the quotes

How do I escape a single quote ( ' ) in JavaScript? [duplicate]

This question already has answers here:
Why are inline event handler attributes a bad idea in modern semantic HTML?
(3 answers)
Single quote escape in JavaScript function parameters
(7 answers)
Closed 9 years ago.
UPDATE:
I want to give an updated answer to this question. First, let me state if you're attempting to accomplish what I have below, I recommend that you manage events by adding event listeners instead. I highly recommend that you utilize jQuery for your project and use their syntax to manage event listeners over using DOM.
QUESTION
Okay, I am basically doing this:
document.getElementById("something").innerHTML = "<img src='something' onmouseover='change(\'ex1\')' />";
I don't want double quotes (") where I put the \'. I only want a single quote, so I am trying to not make it put a double when it is used. I am trying to reach this in the final outcome.
<img src="something" onmouseover="change('ex1')" />
Escaping isn't working for me.
My marked answer works fine, however, the cleaner (and more professional-looking way, IMO) is loganfsmyth's answer.
You should always consider what the browser will see by the end. In this case, it will see this:
<img src='something' onmouseover='change(' ex1')' />
In other words, the "onmouseover" attribute is just change(, and there's another "attribute" called ex1')' with no value.
The truth is, HTML does not use \ for an escape character. But it does recognise " and &apos; as escaped quote and apostrophe, respectively.
Armed with this knowledge, use this:
document.getElementById("something").innerHTML = "<img src='something' onmouseover='change("ex1")' />";
... That being said, you could just use JavaScript quotes:
document.getElementById("something").innerHTML = "<img src='something' onmouseover='change(\"ex1\")' />";
The answer here is very simple:
You're already containing it in double quotes, so there's no need to escape it with \.
If you want to escape single quotes in a single quote string:
var string = 'this isn\'t a double quoted string';
var string = "this isn\"t a single quoted string";
// ^ ^ same types, hence we need to escape it with a backslash
or if you want to escape \', you can escape the bashslash to \\ and the quote to \' like so:
var string = 'this isn\\\'t a double quoted string';
// vvvv
// \ ' (the escaped characters)
However, if you contain the string with a different quote type, you don't need to escape:
var string = 'this isn"t a double quoted string';
var string = "this isn't a single quoted string";
// ^ ^ different types, hence we don't need escaping
You can escape a ' in JavaScript like \'
Since the values are actually inside of an HTML attribute, you should use &apos;
"<img src='something' onmouseover='change(&apos;ex1&apos;)' />";
document.getElementById("something").innerHTML = "<img src=\"something\" onmouseover=\"change('ex1')\" />";
OR
document.getElementById("something").innerHTML = '<img src="something" onmouseover="change(\'ex1\')" />';
It should be working...

need help using quotes in href with javascript

I need help writing a link using js.
This is what I have but I can't get it to jive. Can you help?
document.write('<a href=\"\http://www.domain.com/drowning-accidents/\"\ target=\"\_blank\"\ onClick=\"\trackOutboundLinktarget=(this, 'Outbound Links', 'domain.com'); return false;\"\><img src=\"images/infographic.png\" border=\"0\" class=\"borders\" alt=\"Check out the Infographic\" /></a><br /><br /></center>');
I think it has to do with the single quotes around Outbound... but not sure.
Thanks!
You need to escape the single quotes since the entire string is wrapped in single quotes:
'...(this, \'Outbound Links\', \'domain.com\');...'
^ ^ ^ ^
Technically, escaping the double quotes is not necessary. You only need to escape quotes that are the same type as the quotes used around the entire string.
Also, it appears you are escaping some strange characters:
\"\http://www.domain.com/drowning-accidents/\"\ target=\"\_blank\"\
^^ ?? ^^ ?? ^^ ??
The correct pattern to escape a character, is \x, not \x\. Otherwise, the second \ will get misinterpreted as the start of a second escape sequence.
I hope I found all \ that were too much and the ones missing …
document.write('<img src="images/infographic.png" border="0" class="borders" alt="Check out the Infographic" /><br /><br /></center>');
You have the double quotes escaped, but the whole string is inside single quotes. Just change that to double quotes.
Try this:
document.write("<img src=\"images/infographic.png\" border=\"0\" class=\"borders\" alt=\"Check out the Infographic\" /><br /><br /></center>");
It is not necessary to escape all ' or " within the string. It should escape when you are same quota of the string.
Example: "I'm a boy" this string no need to escape
But look at this example: 'I\'m a boy' this time you have to escape becasue of same quot of string
document.write('<img src="images/infographic.png" border="0" class="borders" alt="Check out the Infographic" /><br /><br />');
Try using this code..
document.write("<a href=\"\http://www.domain.com/drowning-accidents/\"\ target=\"\_blank\"\ onClick=\"\trackOutboundLinktarget=(this, 'Outbound Links', 'domain.com'); return false;\"\><img src=\"images/infographic.png\" border=\"0\" class=\"borders\" alt=\"Check out the Infographic\" /></a><br /><br /></center>");
I guess you have used single quotes instead of double quotes.
Check if it works..:)

Categories