2

Is it possible to check if a JQuery string has a valid syntax? I've tried Jint because of this Determine whether a string contains valid javascript code. Unfortunately, it seems as if this is meant for another use-case.

I have tried the following (which leads to "document is not defined"):

public bool IsValidJqueryString(string jQueryString)
{
     // Validate JQuery
     var executedQuery = new Jint.Engine().Execute(
          @"$(document).ready(function () {
          });"
     );
}
Liam
  • 27,717
  • 28
  • 128
  • 190
Astrophage
  • 1,231
  • 1
  • 12
  • 38

1 Answers1

0

From what I understand (I'm only starting out with Jint) it will run JavaScript, but it doesn't define any variables for you. You have to define up front which variables are going to be available within the JavaScript code (with SetValue()), or define them within the JavaScript code.

So yes, document is not defined since nothing has defined it. Even if you do define it, you may find that it will complain about $ not being a function, since that hasn't been defined either (you haven't loaded the jQuery library).

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84