0

I have an HTML page which has a div tag in it,

<htmL>
   <head/>
      <body>
         <div id ="Required">
            <table>
               <tr>
                <td/>
               </tr>
            </table>
         </div>
      </body>
</html>

While displaying the above website using UIWebView, I need to focus on the div tag as soon as the webpage loads. So basically, the user should see the table(under the div tag) on his iPhone as soon as the url loads under UIWebView.

What have I tried till now?

-(void) ViewDidLoad()
{
   self.webView.delegate = self;
   NSURL *url = [NSURL URLWithString:@"www.nameofthewebsite.com"];
   NSURLRequest *req = [NSURLRequest requestWithURL:url];
   [self.webView loadRequest:request];

}

-(void) webViewDidFinishLoad:(UiWebView*) webView{
   self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('Required').focus()];

}

I still do not see my div tag being focussed when the page loads. I also tried using the load html string function. Am I missing something?

Sagar
  • 101
  • 1
  • 1
  • 11
  • What do you mean by "focus"? You can't normally focus a DIV element because it doesn't take user input. – aapierce Feb 17 '14 at 22:29
  • By focus I mean, the area of the iphone screen should be covered by the table because the table has the most important information for the user. Rest elements can be hidden and be viewed by moving the page. Hopefully, this explanation helps. Is there a way I can focus such static elements which just give information and doesn't take user input? – Sagar Feb 17 '14 at 23:04

1 Answers1

0

If you're trying to get the web view to automatically scroll so that your <div> is front and center, focus() is not the right tool for the job. focus() tells the browser to prepare an element for user input, and is (usually) only used on <input> elements (and its cousins like <textarea> and <select>)

The function you're looking for is scrollIntoView().

aapierce
  • 897
  • 9
  • 14
  • The example I found on stackoverflow [Scroll to text on UIWebView](http://stackoverflow.com/questions/9324927/scroll-to-text-on-uiwebview) refers to a website which doesn't exist anymore. Also, it refers to modifying the original html file which I don't have access to. Any thoughts? – Sagar Feb 27 '14 at 18:34
  • Did `scrollIntoView()` not work for you? You would use that instead of `.focus()`. You don't need to modify the HTML document. – aapierce Feb 27 '14 at 20:03