Reputation: 85
Here is my javascript file below and i am getting the error "Error: TypeError: Cannot set property 'innerHTML' of null" . What changes should i make so that it works fine. Thanks in advance. Please help.
var scanCode = function() {
window.plugins.barcodeScanner.scan(
function(result) {
alert("Scanned Code: " + result.text
+ ". Format: " + result.format
+ ". Cancelled: " + result.cancelled);
document.getElementById("d").innerHTML="result.text";
window.location.href = 'page5.html';
}, function(error) {
alert("Scan failed: " + error);
});
}
I am getting the error as:
06-24 11:55:08.130: W/FlashlightManager(7817): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 11:55:08.130: W/FlashlightManager(7817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-24 11:55:08.130: W/FlashlightManager(7817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-24 11:55:08.130: W/FlashlightManager(7817): at dalvik.system.NativeStart.main(Native Method)
06-24 11:55:08.540: D/OpenGLRenderer(7817): Flushing caches (mode 0)
06-24 11:55:08.870: D/OpenGLRenderer(7817): Flushing caches (mode 0)
06-24 11:55:14.920: D/PhoneGapLog(7817): Error in success callback: BarcodeScanner2 = TypeError: Cannot set property 'innerHTML' of null
06-24 11:55:14.920: D/PhoneGapLog(7817): file:///android_asset/www/phonegap-1.4.1.js: Line 692 : Error in success callback: BarcodeScanner2 = TypeError: Cannot set property 'innerHTML' of null
06-24 11:55:14.930: I/Web Console(7817): Error in success callback: BarcodeScanner2 = TypeError: Cannot set property 'innerHTML' of null at file:///android_asset/www/phonegap-1.4.1.js:692
here is the HTML code where i am calling the above js function
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
<title>Bar Code Scanner</title>
<link rel="stylesheet" href="main.css" type="text/css">
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" src="barcodescanner.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body id="stage" class="theme">
<h1>Barcode Scanner and Encoder</h1>
<h2>PhoneGap Barcode Scanner Plugin</h2>
<a href="#" class="btn" onclick="scanCode();">Scan Code</a>
</body>
</html>
and here is another html file where i need to print the result.
<!doctype html>
<html>
<head>
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<p id="d"></p>
</body>
</html>
Upvotes: 0
Views: 1083
Reputation: 14315
Try this ::
Page1.html:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
<title>Bar Code Scanner</title>
<link rel="stylesheet" href="main.css" type="text/css">
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" src="barcodescanner.js"></script>
</head>
<body id="stage" class="theme">
<h1>Barcode Scanner and Encoder</h1>
<h2>PhoneGap Barcode Scanner Plugin</h2>
<a href="#" class="btn" onclick="scanCode();">Scan Code</a>
</body>
<script type="text/javascript">
var scanCode = function () {
window.plugins.barcodeScanner.scan(
function (result) {
alert("Scanned Code: " + result.text + ". Format: " + result.format + ". Cancelled: " + result.cancelled);
localStorage.setItem("myvalue", result.text);
window.location.href = 'page2.html';
}, function (error) {
alert("Scan failed: " + error);
});
}
</script>
</html>
Page2.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Checkd Control</title>
<meta charset="UTF-8">
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
</head>
<body>
<p id="d"></p>
</body>
<script type="text/javascript">
var barcodeVal = localStorage.getItem("myvalue");
document.getElementById("d").innerHTML = barcodeVal;
</script>
</html>
Upvotes: 1