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..:)
Related
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"!`);
jQuery('#Level_of_Interest__c option[value="Bachelor's"]').attr("selected","selected")
this is the code I am trying to run but because of the " ' " on Bachelor's I am getting a error, any one has any ideas?
You can use backslash:
//since \ itself is a escape character you need to use \\
jQuery('#Level_of_Interest__c option[value="Bachelor\\'s"]')
.attr("selected","selected")
single backslash is enough.
$('#Level_of_Interest__c option[value="Bachelor\'s"]').attr("selected","selected")
You simply need to escape it using backslash
jQuery('#Level_of_Interest__c option[value="Bachelor\'s"]').attr("selected","selected");
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/\">"
I am creating a bookmarklet and I have to avoid using double quotes ( " ) because I have to include the bookmarklet inside a HTML page and it's inside a string that uses double quotes:
<a href="javascript:...">
In some places I have to use the code replace(/".*/, '');
In sed, I can escape double quotes using \x22 (22 is the ASCII hex code for double quotes)
Is it possible to do something similar in JavaScript like replace(/\x22.*/, '');?
You can use the other syntax for making a RegExp and use String.fromCharCode
Here's an example:
'"hello"'.replace(new RegExp(String.fromCharCode(34), 'g'), '-quotes-')
-->
'-quotes-hello-quotes-'
The same way you would use quotes in any HTML attribute's value.
example
<img src="..." alt="A picture of Bob "e;Wild Man" Johnson">
so for you
<a href="javascript:replace(/".*/, '');">
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 ' 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 '
"<img src='something' onmouseover='change('ex1')' />";
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...