4

I am trying to use jQuery functions in JavaScriptCore, so I have executed the jQuery library as a script in my JSContext. However, after I do that, I still can't access any jQuery functions. Here's a stripped-down version of my code in a Xcode playground:

import JavaScriptCore
let context = JSContext()

//Evaluate JQuery library in context:
do {
let libString = try String(contentsOf: URL(string: "http://code.jquery.com/ui/1.9.2/jquery-ui.js")!, encoding: .utf8)
context?.evaluateScript(libString)
} catch (let error) {
    print("Error while processing URL: \(error)")
}

//Access variables defined by JQuery library:
 print("'$' in context is defined as: \(context?.objectForKeyedSubscript("$"))")
 print("'jQuery' in context is defined as: \(context?.objectForKeyedSubscript("jQuery"))")

This prints:

'$' in context is defined as: Optional(undefined)

'jQuery' in context is defined as: Optional(undefined)

If I had included the library correctly, those would be defined as functions. What have I done wrong? Should I even be using JavaScriptCore to gain jQuery functionality?

Rodia
  • 1,407
  • 8
  • 22
  • 29
E. Tang
  • 41
  • 3
  • Possible duplicate of [How to include a external library in JavaScriptCore?](http://stackoverflow.com/questions/23397658/how-to-include-a-external-library-in-javascriptcore) – Vaishal Patel Mar 23 '17 at 07:41

1 Answers1

-1

You should include jQuery library:

NSString *commond = @"window.onload=function(){var jq=document.createElement(\"script\");jq.setAttribute(\"src\",\"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/js/lib/jquery-1.10.2.js\");document.getElementsByTagName(\"head\")[0].appendChild(jq);window.setTimeout(\"exec()\",1000)};function exec(){document.write($(\"body\").get(0))};";

[self.webView stringByEvaluatingJavaScriptFromString:commond];
Solomiya
  • 310
  • 4
  • 15
Amos_
  • 9
  • 2