admin管理员组文章数量:1278858
I need to pre-fill text fields in a UIWebView
and am given to understand that javascript
is the best way to go about this. Unfortunately I know nothing of javascript
and have been fumbling about for the last few hours, getting nowhere.
Latest botched attempt:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// username field id = username_5 & pw field id = password_5
NSString *javascript = @"\
var user = 'testUser';\
var pw = 'testPW';\
document.getElementById('username_5').value = user; \
document.getElementById('password_5').value = pw; \
;";
// Execute JS
[_emailWebView stringByEvaluatingJavaScriptFromString:javascript];
}
Can anyone point me in the right direction?
-EDIT-
I also tried tried delaying the call in case the page had not fully loaded and if I call something like:
- (void)webViewDidFinishLoad:(UIWebView *)wv
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(injectJavascript) withObject:nil afterDelay:1.0];
}
- (void)injectJavascript
{
[self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('password_5').value = 'testPW';"]];
[self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.alert('test');"]];
}
the alert appears but the field with ID password_5
is not filled.
The fields in question are nested within a form. Don't know if that makes any difference?
-EDIT 2-
I'm pretty sure the problem is related to the HTML / nesting on the target website as I just tried this on another site and it worked.
The target site is nested like this:
<html>
<head> … </head>
<body onload="FinishLoad(1);hideJSWarn();">
<div id="noJSWarn" class="cssSecurityWarning" style="display: none;"> … </div>
<table id="table_LoginPage_1" > … </table>
<table id="table_LoginPage_2" > … </table>
<blockquote>
<form id="frmLogin_4" onsubmit="return Login(1)" autoplete="off" method="POST" action="login.cgi" name="frmLogin">
<input id="tz_offset_5" type="hidden" name="tz_offset"></input>
<table id="table_LoginPage_3" >
<tbody>
<tr> …
<td valign="top">
<table id="table_LoginPage_6" >
<tbody>
<tr>
<td> … </td>
<td>
<input id="username_5" type="text" size="20" name="username"></input>
I need to pre-fill text fields in a UIWebView
and am given to understand that javascript
is the best way to go about this. Unfortunately I know nothing of javascript
and have been fumbling about for the last few hours, getting nowhere.
Latest botched attempt:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// username field id = username_5 & pw field id = password_5
NSString *javascript = @"\
var user = 'testUser';\
var pw = 'testPW';\
document.getElementById('username_5').value = user; \
document.getElementById('password_5').value = pw; \
;";
// Execute JS
[_emailWebView stringByEvaluatingJavaScriptFromString:javascript];
}
Can anyone point me in the right direction?
-EDIT-
I also tried tried delaying the call in case the page had not fully loaded and if I call something like:
- (void)webViewDidFinishLoad:(UIWebView *)wv
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(injectJavascript) withObject:nil afterDelay:1.0];
}
- (void)injectJavascript
{
[self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('password_5').value = 'testPW';"]];
[self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.alert('test');"]];
}
the alert appears but the field with ID password_5
is not filled.
The fields in question are nested within a form. Don't know if that makes any difference?
-EDIT 2-
I'm pretty sure the problem is related to the HTML / nesting on the target website as I just tried this on another site and it worked.
The target site is nested like this:
<html>
<head> … </head>
<body onload="FinishLoad(1);hideJSWarn();">
<div id="noJSWarn" class="cssSecurityWarning" style="display: none;"> … </div>
<table id="table_LoginPage_1" > … </table>
<table id="table_LoginPage_2" > … </table>
<blockquote>
<form id="frmLogin_4" onsubmit="return Login(1)" autoplete="off" method="POST" action="login.cgi" name="frmLogin">
<input id="tz_offset_5" type="hidden" name="tz_offset"></input>
<table id="table_LoginPage_3" >
<tbody>
<tr> …
<td valign="top">
<table id="table_LoginPage_6" >
<tbody>
<tr>
<td> … </td>
<td>
<input id="username_5" type="text" size="20" name="username"></input>
Share
Improve this question
edited Aug 15, 2013 at 12:41
Robert
asked Aug 9, 2013 at 11:37
RobertRobert
5,30243 gold badges68 silver badges115 bronze badges
3
-
1
Im not sure if it will help, but aren't u missing
;
after the'testPW'
? – Lukas Kukacka Commented Aug 9, 2013 at 11:41 -
1
I just tested your code (with the added
;
I suggested in previous ment) and your JS code works fine for me. So maybe check your HTML code if its OK – Lukas Kukacka Commented Aug 9, 2013 at 11:58 - Still not working so may well be the HTML. – Robert Commented Aug 9, 2013 at 13:09
3 Answers
Reset to default 5You have to put the javascript code inside one NSString and then execute with stringByEvaluatingJavaScriptFromString
. This is how the code would look like:
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('email').value = '%@'", email]];
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('pass').value = '%@'", password]];
I had obtained the element IDs by highlighting them in Firefox and selecting inspect element
.
Turns out this gives different IDs to those returned with:
NSString *body = [self.emailWebView stringByEvaluatingJavaScriptFromString:
@"document.getElementsByTagName('body')[0].outerHTML"];
NSLog(@"%@", body);
Plugging these IDs (which apparently vary dependent upon what was used to navigate to the page) into:
[self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('username').value = '%@';", user]];
[self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('password').value = '%@';", pw]];
[self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('input_LoginPage-ipad_1').click();"]];
Solved it.
Thanks for all your help folks.
Use this Code This will Work
view.loadUrl("javascript:(function() { "
+ " document.getElementById('login').value = '" + emailAdress+ "';"
+ " document.getElementById('login').readOnly='true'"
+ "})();");
本文标签: javascriptPrefill WebView text fieldsStack Overflow
版权声明:本文标题:javascript - Pre-fill WebView text fields - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741291631a2370582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论