passing an element from one UIWebView to another


I am making an app that uses two UIWebViews. On one of them i strip the webpage for javascript and load it (to make the page appear faster). The other one loads the full page with javascript in the background and replaces the other UIWebView when it's done. The page with javascript will have some added banners and stuff making it bigger.

Im trying to make the UIWebView automatically scroll to the same place (element) where the user were on the other WebView. Ive been trying to do this through javascript (using document.elementFromPoint), but i cant find a way to pass an Element to the other UIWebView (dont know if its even possible). I can't locate the element through Id either becuase they dont have Id atributes.

Is there a way to pass an element as a variable from one UIWebView to another? or is there another way to do this?


You can use - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script for passing parameters.

The javascript method can return the string which is then returned by the above method. It can return the data in form of JSON for multiple values which you can parse then using JSON parser. Or it can simply return a string. You can use that string/JSON-data further.

To pass the value see following code :

NSString *jsPrintBodyOfTable=[[NSString alloc]initWithFormat:@”printBodyOfTable((\”%@\”))”, content.at_content];

 [self.webView stringByEvaluatingJavaScriptFromString:jsPrintHeadOfTable];

Simple tutorial at

By : san

It is not possible to plainly pass it from one UIWebView to another.

However, if you define a protocol of your own and pass your data as a URL, you can pass data to the native environment, then you can extract and execute Javascript on the other web view to inject the data.

Catching the URL can be done with webView:shouldStartLoadWithRequest:navigationType: in a class that implements UIWebViewDelegate protocol, and execute Javascript can be done with stringByEvaluatingJavaScriptFromString: on the UIWebView instance.

This is what is done in PhoneGap/Cordova, if you look at the source code.

By : nhahtdh

There is a DOM property which is what your are looking for: scrollY, so you can retrieve it in your first web view:

NSString* scrollPosition = [webView1 stringByEvaluatingJavaScriptFromString: @"scrollY"];

and then make the second scroll to it:

[webView2 stringByEvaluatingJavaScriptFromString:
     [NSString stringWithFormat: @"window.scrollTo(0, %d);", scrollPosition]];
By : sergio

This video can help you solving your question :)
By: admin