Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  179] [ 2]  / answers: 1 / hits: 6979  / 10 Years ago, fri, december 26, 2014, 12:00:00

while passing string parameters from java to javascript function it breaks if parameters contains space in it.



String str = This is test;
sb.append(<a href=javascript:testFunction(+str+;>test</a>);


then while clicking on this hyper link it gives below error as:
SyntaxError: unterminated string literal


More From » string

 Answers
12

You can't use characters in an HTML attribute value which doesn't have delimiters and you are missing a ).



You could add the missing bits:



sb.append(<a href='javascript:testFunction(+str+);'>test</a>);


But you would be better off:




  • Not constructing JavaScript by mashing together strings

  • Not constructing HTML by mashing together strings

  • Not using a link to run JavaScript



Such:



var button = document.createElement('button');
button.type = button;
button.appendChild(
document.createTextNode('test')
);
button.addEventListener('click', function (event) {
testFunction(str);
});
ab.append(button); // Assuming that whatever `ab.append` is accepts a DOM element

[#40391] Wednesday, December 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brynn

Total Points: 448
Total Questions: 83
Total Answers: 118

Location: Albania
Member since Sun, Nov 22, 2020
4 Years ago
brynn questions
Wed, Sep 22, 21, 00:00, 3 Years ago
Wed, Dec 23, 20, 00:00, 4 Years ago
Wed, Aug 12, 20, 00:00, 4 Years ago
;