如何控制 UIWebView 的 javascript [英] How to control UIWebView's javascript

查看:19
本文介绍了如何控制 UIWebView 的 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就是 UIWebview 和 UIView 上的切换按钮.

It is that UIWebview and toggle button on UIView.

网页包含 javascript.

WebPage includes javascript.

我想控制 UIWebView 的 javascript.

I want to control UIWebView's javascript.

我想要的是,每当 UIView 上的 Button 被切换时,javascript 就会打开或关闭.

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

有可能吗?

如果是这样,我该怎么办?很遗憾我不知道.

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

这是我的javascript代码:

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]; 

}

推荐答案

不要认为可以为 UIWebView 禁用 JavaScript.

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

但是你可以使用UIWebView的stringByEvaluatingJavaScriptFromString:方法,来执行特定的JS代码,所有一个JS函数等

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;

例如:

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

如果您想停用您设置的 onclick(),例如,您可以使用 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;
    ...

所以:

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

这篇关于如何控制 UIWebView 的 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆