Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  55] [ 3]  / answers: 1 / hits: 32673  / 15 Years ago, fri, march 6, 2009, 12:00:00

I am having problem with escaping the single and double quotes inside the hrefs JavaScript function.



I have this JavaScript code inside href. It's like -



<a href = javascript:myFunc(fileDir/fileName.doc, true)> click this </a>


Now, since double quotes inside double quote is not valid, I need to escape the inner double quotes for it to be treated as part of the string -
so, I need to do this -



<a href = javascript:myFunc(fileDir/fileName.doc , true)> click this </a>


The problem is, even the above code is not working. The JavaScript code is getting truncated at -- myFunc(



I tried with the single quote variation too - but even that doesn't seem to work (meaning that if I have a single quote inside my string literal then the code gets truncated).



This is what I did with a single quote:



<a href = 'javascript:myFunc(fileDir/fileName.doc , true)'> click this </a>


This works, but if I have a single quote inside the string then the code gets truncated in the same way as that of double quotes one.


More From » escaping

 Answers
35

Using backslashes to escape quotes is how it works in JavaScript, but you're not actually writing JavaScript code there: you're writing HTML. You can do it by using the HTML escaping method: character entities.



&quot;  // 
&#39; // '


For example:



<a href=javascript: alert('John O&#39;Brien says &quot;Hi!&quot');>...</a>

[#99885] Friday, February 27, 2009, 16 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emanir

Total Points: 151
Total Questions: 90
Total Answers: 105

Location: Suriname
Member since Sun, Jun 13, 2021
3 Years ago
;