I am trying to inject jQuery into UIWebViewpages that I cannot control, such as Google. When a UITextFieldgets focus, I execute the following lines of code:
NSString* scriptInject = @"var headElement = document.getElementsByTagName('head')[0]; var script = document.createElement('script');
script.setAttribute('src','http://jquery.com/src/jquery-latest.js');
script.setAttribute('type','text/javascript'); headElement.appendChild(script);";
[myWebView stringByEvaluatingJavaScriptFromString:scriptInject];
[myWebView stringByEvaluatingJavaScriptFromString:@"$(document).ready(function() {$('body').css('background', '#FF0000');}"];
I am doing a background change to verify that I can run a jQuery script. I can verify that the Objective-C method is being called, and I even set the alertnewly created tag to load <script>, so I know that it runs in UIWebView. How do I insert it correctly to execute it?
EDIT:
I have an event by trying this after calling a function to change color:
[myWebView stringByEvaluatingJavaScriptFromString:@"alert($('body').css());"];
There are no warnings.
source
share