Jim
Jim

Reputation: 19572

Syntax error when trying to pass second argument to JS function

The following works:

loadUrl("javascript:HtmlViewer.showHTML" + "('<html>'+document.forms[0].elements['MYHiddenValue'].value+'</html>', 'blah');");

The following:

loadUrl("javascript:HtmlViewer.showHTML" + "('<html>'+document.forms[0].elements['MYHiddenValue'].value+'</html>', 'document.forms[0].elements['MYHiddenValue'].value');");

or

loadUrl("javascript:HtmlViewer.showHTML" + "('<html>'+document.forms[0].elements['MYHiddenValue'].value+'</html>', 'document.forms[0].elements['MYSecondHiddenValue'].value');");`

Gives:

[INFO:CONSOLE(1)] "Uncaught SyntaxError: missing ) after argument list", source:  (1)

What am I doing wrong?

Upvotes: 0

Views: 116

Answers (3)

Bulent Vural
Bulent Vural

Reputation: 2688

Replace all ’ characters with '

(Copy / paste from here as they look like each other)

Edit after the comment from OP below.

First failing command :

loadUrl("javascript:HtmlViewer.showHTML" + "('<html>'+document.forms[0].elements['MYHiddenValue'].value+'</html>', 'document.forms[0].elements['MYHiddenValue'].value');");

This has a pair of single quotes which will cause a syntax error when the line executed and produced the string. It should produce an escaped pair of single quotes so the line should contain an 'escaped' backslash:

loadUrl("javascript:HtmlViewer.showHTML" + "('<html>'+document.forms[0].elements['MYHiddenValue'].value+'</html>', 'document.forms[0].elements[\\'MYHiddenValue\\'].value');");

Second option would be using double quotes and escaping each with a single backslash at the first stage like :

loadUrl("javascript:HtmlViewer.showHTML" + "('<html>'+document.forms[0].elements['MYHiddenValue'].value+'</html>', 'document.forms[0].elements[\"MYHiddenValue\"].value');");

Second command has also a similar error on 'MYSecondHiddenValue'.

The reason that first command is not failing is that it is building the html string by adding three separate strings which is not causing a quote issue.

But this also makes the three commands differ each other. First command adds the value of document.forms[0].elements['MYHiddenValue'] and the other two adds the string "document.forms[0].elements...value". So we don't know which one is correct without knowing what showHTML() does exactly.

Upvotes: 2

Chris van Hasselt
Chris van Hasselt

Reputation: 121

I believe this is the problem. In your first (working) example,

loadUrl("javascript:HtmlViewer.showHTML" + "('<html>'+document.forms[0].elements[’MYHiddenValue’].value+'</html>', 'blah');");

the second embedded parameter 'blah' does not have any internal quotes. However, in your first non-working example,

loadUrl("javascript:HtmlViewer.showHTML" + "('<html>'+document.forms[0].elements[’MYHiddenValue’].value+'</html>', 'document.forms[0].elements[’MYHiddenValue’].value');");

there is an embedded quote in a section of the parameter that should be evaluated. The following sub-string (found in both examples has a string ('') concatenated with code that evaluates to a string concatenated with another string, the closing HTML tag.

'<html>'+document.forms[0].elements[’MYHiddenValue’].value+'</html>'

In the non-working second example,

 'document.forms[0].elements[’MYHiddenValue’].value'

You have a string that I believe just needs to be evaluated. JavaScript is seeing this as a string like as below, but this is not what you want.

 'document.forms[0].elements[’

I believe if you remove the outer pair of single-quote marks in this non-working example, it will work, as the code will evaluate. If you need it to just be a string, then you need to escape the inner single-quote marks as in

 'document.forms[0].elements[\'MYHiddenValue\'].value'

Upvotes: 1

David Wolters
David Wolters

Reputation: 336

Well, looking at your code right now, I can see that

loadUrl("javascript:HtmlViewer.showHTML" + "('<html>'+document.forms[0].elements[’MYHiddenValue’].value+'</html>', 'document.forms[0].elements[’MYHiddenValue’].value');");

Is really one big string, (or two that are concatinated), try changing it to:

loadUrl("javascript:HtmlViewer.showHTML" + "('<html>'+document.forms[0].elements[’MYHiddenValue’].value+'</html>'", "'document.forms[0].elements[’MYHiddenValue’].value');");

perhaps?

Upvotes: 0

Related Questions