1

I want to know which button is called of javascript in uiwebview, My script is

 function nextPage()
 {
   window.location = "page3.html";
 }
function prevPage()
 {
      window.location = "page1.html";
 }

My current page is page2.html.On button action, current page will move backword and forward

I set name and onclick action to both buttons

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

how to know which button is called. also want the value inside window.location on button click action

Thanks in advance

1 Answer 1

1

In your nextPage and prevPage javascript methods, create a custom url that you can parse inside your web view delegate:

function nextPage()
{
    window.location.href = "file://yourappname?nextPage";
}
function prevPage()
{
     window.location.href = "file://yourappname?prevPage";
}

Then in your Web View delegate, implement 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;            
}

Of course that's just a simple example - in real code you'll want to replace @"Page1.html" and @"Page3.html" with code to keep track of the current page and build your url string to page forward or back accordingly.

Sign up to request clarification or add additional context in comments.

4 Comments

i run above code ,also do change in script,but show suffix =currentpage.html it now show "prevPage" or "nextPage".do you have any any another option ?
Not sure I understand - can you post the contents of urlString (request.URL.absoluteString)?
this is the content of (request.URL.absoluteString).My current page is page2.html /Users/macuser/Library/Application Support/iPhone Simulator/5.0/Applications/A16998AD-105C-43E9-A6F5-505FAFABBB68/htmlevents.app/page2.html.
Is that on the initial page load? Or after you've clicked the button? shouldStartLoadWithRequest will get called both on the initial page load AND when you set window.location.href in your javascript code. The URL you've posted looks like the initial page load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.