Javascript: unterminated statement with long strings

Hello,

today I was experimenting with the BaseElements plugin and its Javascript function and had the following issue:

BE_EvaluateJavaScript (

"
var a = ' " & $String & " ';
a
")

works if the string is short, but on a very long string I get:

"SyntaxError: unterminated statement (line 1)"

Any idea on how to fix this?

EDIT: I forgot to mention that the string does not include line breaks.

Regards,

Matteo

Break the line in two with plus to connect them when evaluating.

Or if possible set value into global property for JavaScript as with MBS Plug-in.

1 Like

Thank you very much for you suggestions.

The string in my example is around 17 000 characters long. How do I know where I have to break it up?

"set value into global property for JavaScript"

I don't understand this sugesstion. Could you explain a bit more?

Well, you could make a few tests with shorter texts until you come under the limit and know how big it is.

:slight_smile:

Yes, of course.

It is 9262 characters.

Do you mean something like this?

BE_EvaluateJavaScript (
"
var a = '" & Left ( $String ; 9000 ) & "'+'" & Middle ( $String ; 9001 ; 300 ) & "';
a
")

Unfortunately it does not work and gives the same error. :slightly_frowning_face:

Hi Matteo,

There are some characters which, if present in the value of $String, could break the code, perhaps yielding the symptoms that you are seeing.

Usually, before inserting a value into some Javascript, I will pass the value through a Substitute function to properly perform any needed escaping.

I know that you mentioned that the string contains no line breaks, but perhaps it is another character that is the issue?

The gist of the substitution that I use is something along these lines:

Let([

  ~input = $value ;

  _BS = "\\" ;
  _SQ = "'"  // Single quote

];

   Substitute( ~input ;
      [ _BS ; _BS & _BS ];
      [ Char( 10 ); _BS & "n" ];
      [ Char( 13 ); _BS & "r" ];
      [ Char( 9 ); _BS & "t" ];
      [ _SQ ; _BS & _SQ ]
  )
)

Finally, I will note that of the three plugins that I might reach for (MBS, BaseElements, bBox), I've noted in the past that each seems to support a slightly different vintage of what is possible in the Javascript language. Though each one always wound up meeting my needs, I sometimes needed to adapt code slightly in order to have the same code run in an arbitrary choice of these plugins. This may no longer be the case, and it also may not apply to your particular use case, but I thought it might be worth calling out in case you find yourself wanting to port your work from one plugin to another.

HTH.

1 Like

Hello Steve,

thank you very much.

In my case this line of your substitute function fixed the issue:

[ _SQ ; _BS & _SQ ]

1 Like