How to control UIWebView's javascript

2019-07-24 12:45发布

问题:

It is that UIWebview and toggle button on UIView.

WebPage includes javascript.

I want to control UIWebView's javascript.

What I want is that whenever Button on UIView is toggled, javascript is turned on or off.

Is it possible?

If so, how can i do? It is sad that I don't have idea.

This is my javascript code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *var = nil; 
    var = @"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>"
    "<html>                                        "
    " <head>                                       "
    "  <title> New Document </title>               "
    "<script type='text/javascript'>               "
    "function colorChange(){                       "
    "    var id=document.getElementById('yellow'); "
    "    var backcolor = id.style.backgroundColor; "
    "                                              "
    "    if (backcolor != '')                      "
    "    {                                         "
    "        id.style.backgroundColor = '';        "
    "    }else{                                    "
    "        id.style.backgroundColor = '#ffff00'; "
    "    }                                         "
    "                                              "
    "}                                             "
    "</script>                                     "
    " </head>                                      "
    " <body>                                       "
    "<script type='text/javascript'>               "
    "</script>                                     "
    "                                              "
    "<span id='yellow' ><a href=\"javascript:void(0)\" onclick='colorChange();'><font size=12>text</font></a></span></head></html>"; 

    [self.webView loadHTMLString:var baseURL:nil]; 

}

回答1:

Don't think it's possible to disable JavaScript for a UIWebView.

But you can use the stringByEvaluatingJavaScriptFromString: method of UIWebView, in order to execute specific JS code, all a JS function, etc.

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;

For instance:

[ self.webView stringByEvaluatingJavaScriptFromString: @"colorChange();" ];

If you want to deactivate the onclick() you've set, you can for instance set a global variable with stringByEvaluatingJavaScriptFromString:, and check for that variable in your colorChange function. If it has some value, do something, otherwise, do nothing.

var colorChangeEnabled = true;
function colorChange()
{
    if( colorChangeEnabled == false ) return;
    ...

And so:

[ self.webView stringByEvaluatingJavaScriptFromString: @"colorChangeEnabled = false;" ];


回答2:

Two part question, really.

If you're doing a user-interaction in javascript, it cannot subsequently broker a call into the iOS SDKs. You can do the reverse -- cause an action to occur within the webview from an iOS element, but you can't do the reverse.

Second, there's no public API for disabling/enabling javascript within an UIWebView object.

You can turn it on/off for Safari, so there's a way to disable the engine, but it seems to be a private method, so it's use will be prohibited in any app store submission.