javascript无法在UIWebview的按钮onsubmit中工作,因为safari工作正常?

时间:2014-03-31 12:35:30

标签: javascript ios objective-c uiwebview

我正在我的应用程序中使用UIWebview加载请求http url。网页加载的网址没有失败,但是哪个按钮的操作作为java脚本。问题是onclick提交没有任何反应在控制台和uiwebview代理也没有错误解雇,包括错误委托。该按钮就像图像视图一样。提交按钮onclick在移动safari应用程序中响应良好的地方。下面我附上了我的http url的部分视图源代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"><link href="/App_Themes/Common.css" rel="Stylesheet" type="text/css" />
    <script src="/Script/siteCommon.js" type="text/javascript" ></script>


<link href="../../App_Themes/default/style.css" type="text/css" rel="stylesheet" /><title>
    Change Password
</title></head>
<body>
    <form method="post" action="10470" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="_TSM_HiddenField_" id="_TSM_HiddenField_" value="2GFwlGU9ATlFIxrdsXRzcja58_1t5F8HSleaZM4ZQwk1" />
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE5NDU5NTY1MjYPZBYCZg9kFgICAw9kFgICBw9kFggCAw9kFgICAQ8WAh4EVGV4dAXPAVBsZWFzZSBwcm92aWRlIHRoZSBlbWFpbCBhZGRyZXNzIHlvdSB1c2VkIHRvIHJlZ2lzdGVyIHlvdXIgYWNjb3VudCBhbG9uZyB3aXRoIHlvdXIgY3VycmVudCBwYXNzd29yZC4gIFdoZW4gY2hhbmdpbmcgeW91ciBwYXNzd29yZCwgcGxlYXNlIGJlIHN1cmUgdG8gZm9sbG93IHRoZSBwYXNzd29yZCBydWxlcyBzZXQgZm9ydGggYnkgeW91ciBhZG1pbmlzdHJhdG9yLmQCBQ9kFgICAQ8PFgIfAAUORW1haWwgQWRkcmVzczpkZAIHD2QWBmYPZBYCAgEPDxYCHwAFDU9sZCBQYXNzd29yZDpkZAICDw8WAh8ABQlQYXNzd29yZDpkZAIHDw8WAh8ABRFDb25maXJtIFBhc3N3b3JkOmRkAgkPZBYEAgEPDxYEHwAFDlJlc2V0IFBhc3N3b3JkHgdWaXNpYmxlaGRkAgIPDxYEHwAFBlN1Ym1pdB8BZ2RkZKpTQvPJfWmJqFvdHZIrXesCKRnEIKkDGkSgeZv0B58z" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<script src="/WebResource.axd?d=0yNOlELC71f7MU1kDN0EjQ2&amp;t=635152212760076874" type="text/javascript"></script>


<script src="/ScriptResource.axd?d=c93c_YnvR2d5xbofY3U48Vv2IRMVoKIPk_hrlJ8-C5LxDZb6nfS51RYyYBFDJDWwblnlsXZ0-KwhxpojwLpoQQ2&amp;t=1e961a8d" type="text/javascript"></script>
<script src="/ScriptResource.axd?d=CBZ_3hbD_tfINaKqOHxpx7JALH5uYYEO3oWKa0RYfcnTbdSBTlY7W2hGWTKE5diJ0&amp;t=ffffffff81f1a403" type="text/javascript"></script>
<script src="/ScriptResource.axd?d=CBZ_3hbD_tfINaKqOHxpx7JALH5uYYEO3oWKa0RYfcmlwqDqfxGdA2J9XRFKE5rz0&amp;t=ffffffff81f1a403" type="text/javascript"></script>
<script src="/actions/password/10470?_TSM_CombinedScripts_=True&amp;v=2GFwlGU9ATlFIxrdsXRzcja58_1t5F8HSleaZM4ZQwk1&amp;_TSM_Bundles_=&amp;cdn=False" type="text/javascript"></script>
<script src="/ScriptResource.axd?d=CBZ_3hbD_tfINaKqOHxpxwkS3UqxfzxcZj62hGX0hvK9oQFljrnoDHbkkd4RNQkVAwNoEXOsrlyOHfAU-XrwAw2&amp;t=ffffffff81f1a403" type="text/javascript"></script>
<script src="/ScriptResource.axd?d=CBZ_3hbD_tfINaKqOHxpxyqbdySWicIlJxGXGKbjz5V_OOL5bgqUM-2ewmqJqIBS_FJCzNDx5yt8EYyx12l3wQ2&amp;t=ffffffff81f1a403" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
</script>

1 个答案:

答案 0 :(得分:3)

如果任何脚本要使用ajax请求执行某些工作,那么webView委托方法无法直接跟踪它。要使其工作,您必须在webView中注入此脚本。 在您的应用包中创建一个ajaxHandler.js文件并将此代码粘贴到其中

if(!s_ajaxListener){

    var s_ajaxListener = new Object();
    s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open;
    s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;
    s_ajaxListener.callback = function () {
        window.location='myAjaxHandler://' + this.url;
    };

    XMLHttpRequest.prototype.open = function(a,b) {
        if (!a) var a='';
        if (!b) var b='';
        s_ajaxListener.tempOpen.apply(this, arguments);
        s_ajaxListener.method = a;
        s_ajaxListener.url = b;
        if (a.toLowerCase() == 'get') {
            s_ajaxListener.data = b.split('?');
            s_ajaxListener.data = s_ajaxListener.data[1];
        }
    };

    XMLHttpRequest.prototype.send = function(a,b) {
        if (!a) var a='';
        if (!b) var b='';

        s_ajaxListener.tempSend.apply(this, arguments);
        if(s_ajaxListener.method.toLowerCase() == 'post')s_ajaxListener.data = a;
        s_ajaxListener.callback();
    };
}

这个脚本将会做什么,它会将ajax请求转换为可以由我们的webView跟踪的简单组成的URL方案。现在,在JSHandler方法

中的bundle中,将此脚本加载到全局String变量名viewDidLoad
JSHandler = [[NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"ajaxHandler" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil] retain];

现在在你webViewDidStartLoad:webView:shouldStartLoadWithRequest:navigationType委托方法中使用

注入此脚本
[_webView stringByEvaluatingJavaScriptFromString:JSHandler];

注入脚本后,我们准备跟踪ajax请求。将这些行放在webView:shouldStartLoadWithRequest:navigationType委托方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([[[request URL] scheme] isEqual:@"myAjaxHandler"]) {
        NSString *requestedURLString = [[[request URL] absoluteString] substringFromIndex:[CocoaJSHandler length] + 3];

        NSLog(@"ajax request: %@", requestedURLString);
        return NO;
    }

    return YES;
}

这里我们检查请求加载的请求是否是我们编写的URL方案myAjaxHandler的类型,然后从加载中断它并根据需要处理它。 问我是否需要进一步的帮助。

快乐编码:)