Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  103] [ 4]  / answers: 1 / hits: 17230  / 13 Years ago, tue, may 10, 2011, 12:00:00

This is my code:



<script>
function popupTest(title) {
alert(title);
return false;
}
</script>

<a href= onclick=return popupTest('This is &#039;some&#039; &quot;test&quot; string')><span>Recommend</span></a>


Using Firefox 4 I get the error:



Error: missing ) after argument list
Source Code:
return popupTest('This is 'some' test string')


It's like it's decoding the HTML entities but I don't know why.



Have also tried...



<a href= onclick=return popupTest('This is 'some' test string')><span>Recommend</span></a>


Which gives error:



Error: unterminated string literal
Source Code:
return popupTest('This is 'some'

More From » string

 Answers
16

&#039; is HTML for '. So for the first example the HTML is parsed and the JavaScript engine is passed:



return popupTest('This is 'some' test string')


… and the second ' terminates the string.



On the other hand:



onclick=return popupTest('This is 'some' test string')


Is parsed as:




An onclick attribute with the value return popupTest('This is 'some' followed by some invalid data.




You need to deal with the JavaScript first:



return popupTest('This is 'some' test string')


and then escape it for HTML:



onclick=return popupTest('This is 'some' &quot;test&quot; string')


You would probably be better off using unobtrusive JavaScript and binding the event handlers with JavaScript instead of using intrinsic event attributes.


[#92311] Monday, May 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryonk

Total Points: 161
Total Questions: 116
Total Answers: 107

Location: Albania
Member since Sun, Nov 22, 2020
4 Years ago
bryonk questions
;