Android Webview Always Returns Null for JavaScript Getelementbyid on Loadurl

Android WebView always returns null for javascript getElementById on loadUrl

Finally I found a solution! And it is a really strange behaviour.

First of all you need to specify setDomStorageEnabled(true) on your webview. Otherwise the DOM doesn't work. I wonder why no tutorial gave a hint about. But ok.

myview.getSettings().setDomStorageEnabled(true);

After this I ended up in a white blank page with only the value I set. The strange thing is, that javascript:document.getElementById('myfield').value = 'aaa'; returns a value. Namely the one I set. So a new blank page was created that only contains the string "aaa".

I solved it by modifying the javascript to throw away the return result:

javascript:var x = document.getElementById('myfield').value = 'aaa';

And voilá. It is working.

getElementById returning null on Android WebView (only with local html)

That might be due to the fact that your script tag is declared above the canvas. try to put your tag below the viewer, and it should work

<body>
<h1>Header</h1>
<canvas id="viewer"></canvas>
<script type="text/javascript">
var mypdf = null;
var currentPage = 1;
PDFJS.workerSrc = 'file:///android_asset/pdf.worker.jas';

PDFJS.getDocument('file:///android_asset/pdf.pdf').then(function(pdf) {
mypdf = pdf;
renderPage(currentPage);
});

function renderPage(pageNumber) {
mypdf.getPage(pageNumber).then(function(page) {
var scale = 1;
var viewport = page.getViewport(scale);

var canvas = document.getElementById('viewer');
var context = canvas.getContext('2d');
canvas.height = viewport.height-40;
canvas.width = viewport.width;

var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
});

}
</script>
</body>

Or at least put the call to renderPage below the viewer in an other script tag

Android WebView onPageFinished BUG

In my own project I have been using evaluateJavascript(script,null) in onPageFinished to hide html elements. view.loadUrl() Should work the same way.

If you don't need the function be called at later time you could simplify your JS string and instead of \" try using '.

public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:document.getElementById('imPage').style.display='none';");}

Using loadUrl to run javascript loads different page

If you look at a tool look Squirt it does an anonymous function call. So I am not that versed in javascript, but that anonymous call seems to be the key in that the browser sees it as the current page is making the call itself. So try this roughly:

mWebView.loadUrl("javascript:(function(){document.getElementById('UserName').value='" + txtUser.getText()+"';})()");

Fill form in WebView with Javascript

You should fill the values after the page has been loaded. This is an example using your code:

mWebView.loadUrl(url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String user="u";
String pwd="p";
view.loadUrl("javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';");
}
});

Uncaught TypeError: Cannot set property 'value' of null in Android

I surprised when I find out the solution. I need define the variables for Java script codes. Like following code javascript:var x = & it worked.

mWv.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView mWv, String url) {
super.onPageFinished(mWv, url);

/**
* Pass parametes :
* - New SSID
* - New Security Key
* - Display raw picture
*/
// @"document.getElementsByName('%@')[0].value='%@'"

mWv.loadUrl("javascript:var x = document.getElementById('wifi_ssid').value = '" +
mEtSSID.getText().toString() + "';");

// need check password is correct or not
// - empty or minimum has 8 characters.
// - have both character & number in typed password
if ((mEtSecurityKey.length() == 0
| Utils.checkValidatePassword(mEtSecurityKey, 8)
& Utils.checkValidatePassword(mEtSecurityKey.getText().toString()))) {
mWv.loadUrl("javascript:var y = document.getElementById('wifi_key').value = '" +
mEtSecurityKey.getText().toString() + "';");

if (mCbDisplayRawPicture.isChecked())
mWv.loadUrl("javascript:var z = document.getElementById('displayraw').value='checked'");
else
mWv.loadUrl("javascript:var z = document.getElementById('displayraw').value=''");

// @"document.forms['sendForm'].submit()"
// mWv.loadUrl("javascript:console.log('sendForm: '+document.getElementById('sendForm').value);");
mWv.loadUrl("javascript:document.getElementById('sendForm').submit()");

/**
* Also get new SSID and new Security Key in Card Setting page
*/
IS_BACK_FROM_CHANGE_SSID_AND_SCCURITY_KEY_TO_CARD_SETTING_PAGE = true;

/**
* Finish current Change SSID And Security Key after submit success
*/
finish();
} else
Toast.makeText(
ChangeSSIDAndPasswordPage.this,
getString(R.string.toast_your_password_is_case_sensitive),
Toast.LENGTH_SHORT).show();
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});


Related Topics



Leave a reply



Submit