如何控制 UIWebView 的 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8325841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to control UIWebView's javascript
提问by hyekyung
It is that UIWebview and toggle button on UIView.
就是 UIWebview 和 UIView 上的切换按钮。
WebPage includes javascript.
网页包含 javascript。
I want to control UIWebView's javascript.
我想控制 UIWebView 的 javascript。
What I want is that whenever Button on UIView is toggled, javascript is turned on or off.
我想要的是,每当 UIView 上的按钮被切换时,javascript 就会打开或关闭。
Is it possible?
是否可以?
If so, how can i do? It is sad that I don't have idea.
如果是这样,我该怎么办?很遗憾我不知道。
This is my javascript code:
这是我的 javascript 代码:
- (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];
}
采纳答案by Macmade
Don't think it's possible to disable JavaScript for a UIWebView.
不要认为可以为 UIWebView 禁用 JavaScript。
But you can use the stringByEvaluatingJavaScriptFromString:
method of UIWebView, in order to execute specific JS code, all a JS function, etc.
但是你可以使用stringByEvaluatingJavaScriptFromString:
UIWebView的方法,为了执行特定的 JS 代码,所有的一个 JS 函数等等。
- (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.
如果您想停用onclick()
您设置的 ,您可以例如使用 设置一个全局变量stringByEvaluatingJavaScriptFromString:
,并在您的colorChange
函数中检查该变量。如果它有一些价值,就做点什么,否则什么都不做。
var colorChangeEnabled = true;
function colorChange()
{
if( colorChangeEnabled == false ) return;
...
And so:
所以:
[ self.webView stringByEvaluatingJavaScriptFromString: @"colorChangeEnabled = false;" ];
回答by tkone
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.
如果您在 javascript 中进行用户交互,则它随后无法代理对 iOS SDK 的调用。你可以反过来做——从一个 iOS 元素的 webview 中导致一个动作发生,但你不能做相反的事情。
Second, there's no public API for disabling/enabling javascript within an UIWebView object.
其次,在 UIWebView 对象中没有用于禁用/启用 javascript 的公共 API。
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.
您可以为 Safari 打开/关闭它,因此有一种方法可以禁用引擎,但它似乎是一种私有方法,因此在任何应用商店提交中都将禁止使用它。