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?

1

1 Answer 1

-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];
Sign up to request clarification or add additional context in comments.

2 Comments

Format your answer properly using the editing hints. This should be highlighted as code. Then explain why this code answers the question and how it solves the problem.
The question was about JavaScriptCore, not WebView.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.