如何在uiwebview中捕获javascript按钮动作

如何在uiwebview中捕获javascript按钮动作,javascript,iphone,ios,uiwebview,Javascript,Iphone,Ios,Uiwebview,我想知道uiwebview中javascript的哪个按钮被调用, 我的剧本是 function nextPage() { window.location = "page3.html"; } function prevPage() { window.location = "page1.html"; } 我的当前页面是page2.html。在按钮操作中,当前页面将前后移动 我将name和onclick操作设置为两个按钮 <button onclick="prev

我想知道uiwebview中javascript的哪个按钮被调用, 我的剧本是

 function nextPage()
 {
   window.location = "page3.html";
 }
function prevPage()
 {
      window.location = "page1.html";
 }
我的当前页面是page2.html。在按钮操作中,当前页面将前后移动

我将name和onclick操作设置为两个按钮

<button onclick="prevPage();" id="back"></button>
<div id="counter">Page 1 of 13</div>
 <button onclick="nextPage();" id="next"></button>
</div>

第1页,共13页
如何知道调用了哪个按钮。 还需要window.location上的值单击按钮操作


提前感谢

在您的下一页和上一页javascript方法中,创建一个自定义url,您可以在web视图代理中解析该url:

function nextPage()
{
    window.location.href = "file://yourappname?nextPage";
}
function prevPage()
{
     window.location.href = "file://yourappname?prevPage";
}
然后在Web视图委托中,实现shouldStartLoadWithRequest:

-(BOOL)webView:(UIWebView *)webView 
shouldStartLoadWithRequest:(NSURLRequest *)request 
 navigationType:(UIWebViewNavigationType)navigationType 
{
    NSString *urlString = request.URL.absoluteString;
    if (urlString hasSuffix:@"prevPage"])
    {
        NSURL *url = [NSURL urlWithString:@"Page1.html"];
        NSURLRequest *urlRequest = [NSUrlRequest requestWithURL:url];
        [webView loadRequest:urlRequest];
    }
    else if ([queryString hasSuffix:@"nextPage"])
    {
        NSURL *url = [NSURL urlWithString:@"Page3.html"];
        NSURLRequest *urlRequest = [NSUrlRequest requestWithURL:url];
        [webView loadRequest:urlRequest];
    }
    return YES;            
}

当然,这只是一个简单的例子-在实际代码中,您需要用代码替换@“Page1.html”和@“Page3.html”,以跟踪当前页面,并相应地构建url字符串以向前或向后翻页。

在下一页和上一页javascript方法中,创建一个自定义url,您可以在web视图委托中解析:

function nextPage()
{
    window.location.href = "file://yourappname?nextPage";
}
function prevPage()
{
     window.location.href = "file://yourappname?prevPage";
}
然后在Web视图委托中,实现shouldStartLoadWithRequest:

-(BOOL)webView:(UIWebView *)webView 
shouldStartLoadWithRequest:(NSURLRequest *)request 
 navigationType:(UIWebViewNavigationType)navigationType 
{
    NSString *urlString = request.URL.absoluteString;
    if (urlString hasSuffix:@"prevPage"])
    {
        NSURL *url = [NSURL urlWithString:@"Page1.html"];
        NSURLRequest *urlRequest = [NSUrlRequest requestWithURL:url];
        [webView loadRequest:urlRequest];
    }
    else if ([queryString hasSuffix:@"nextPage"])
    {
        NSURL *url = [NSURL urlWithString:@"Page3.html"];
        NSURLRequest *urlRequest = [NSUrlRequest requestWithURL:url];
        [webView loadRequest:urlRequest];
    }
    return YES;            
}

当然,这只是一个简单的例子-在实际代码中,您需要用代码替换@“Page1.html”和@“Page3.html”,以跟踪当前页面,并相应地构建url字符串以向前或向后翻页。

我运行上述代码,也会更改脚本,但show suffix=currentpage.html现在显示“prevPage”或“nextPage”.你还有其他选择吗?我不太明白-你能发布urlString(request.URL.absoluteString)的内容吗?这是(request.URL.absoluteString)的内容。我的当前页面是page2.html/Users/macuser/Library/Application Support/iPhone Simulator/5.0/Applications/A16998AD-105C-439e9-A6F5-505fabb68/htmlevents.app/page2.html。这是否在初始页面加载中?还是在你点击按钮之后?shouldStartLoadWithRequest将在初始页面加载时以及在javascript代码中设置window.location.href时被调用。您发布的URL看起来像初始页面加载。我运行上述代码,也会在脚本中进行更改,但show suffix=currentpage.html它现在显示“prevPage”或“nextPage”。您还有其他选项吗?我不确定是否理解-您可以发布urlString(request.URL.absoluteString)的内容吗?这是(request.URL.absoluteString)的内容。我的当前页面是page2.html/Users/macuser/Library/Application Support/iPhone Simulator/5.0/Applications/A16998AD-105C-439e9-A6F5-505fabb68/htmlevents.app/page2.html。这是否在初始页面加载中?还是在你点击按钮之后?shouldStartLoadWithRequest将在初始页面加载时以及在javascript代码中设置window.location.href时被调用。您发布的URL看起来像初始页面加载。