[英]How to assign phonegap device.platform value to a variable?
I'd like to assign the value in device.platform to a variable so I can check it later in my program. 我想将device.platform中的值赋给变量,以便稍后在我的程序中检查它。 For example, if the app is running on a BlackBerry I might want to have a custom message appear. 例如,如果应用程序在BlackBerry上运行,我可能希望显示自定义消息。
I used the "element" example found elsewhere to make sure things were working properly (and it does). 我使用了其他地方发现的“元素”示例来确保事情正常运行(并且确实如此)。 But I run into problems when I try assigning just the "device.platform" value to devicePlatform. 但是当我尝试仅将“device.platform”值分配给devicePlatform时,我遇到了问题。
I've been fighting with this for an hour, I must be missing something fairly basic. 我一直在与这个战斗一个小时,我必须遗漏一些相当基本的东西。 Can someone please show me what I'm doing wrong? 有人可以告诉我我做错了什么吗?
Thanks Rob 谢谢Rob
<html>
<head>
<title>PhoneGap</title>
<meta name="viewport" content="width=device-width">
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script language="JavaScript" type="text/javascript">
// wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var element = document.getElementById('deviceProperties');
element.innerHTML = 'Device Name: ' + device.name + '<br />' +
'Device Cordova: ' + device.cordova + '<br />' +
'Device Platform: ' + device.platform + '<br />' +
'Device UUID: ' + device.uuid + '<br />' +
'Device Version: ' + device.version + '<br />';
//
}
</script>
</head>
<body>
<h1>Hello PhoneGap v1.9.0</h1>
<script>
var devicePlatform = device.platform;
document.write("Platform: "+devicePlatform+"<br>");
</script>
<br>
<p id="deviceProperties">Loading device properties...</p>
</body>
</html>
Try putting 试试看
var devicePlatform = device.platform;
document.write("Platform: "+devicePlatform+"<br>");
into the function onDeviceReady() 进入函数onDeviceReady()
This should resolve the issue. 这应该解决问题。
Additionally you will probably want to define a globally scoped variable, such as 'platform' and then assign the device.platform to that variable in the function onDeviceReady(). 此外,您可能希望定义一个全局范围的变量,例如'platform',然后将device.platform分配给函数onDeviceReady()中的该变量。
Example: 例:
var platform = null;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
platform = device.platform;
alert(platform);
}
UPDATE: 更新:
I think this is closer to what you are looking for 我认为这更接近你所寻找的
<html>
<head>
<title>PhoneGap</title>
<meta name="viewport" content="width=device-width">
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script language="JavaScript" type="text/javascript">
// wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var element = document.getElementById('platformarea');
element.innerHTML = device.platform;
//
}
</script>
</head>
<body>
<h1>Hello PhoneGap v1.9.0</h1>
<p id="platformarea"></p>
</body>
</html>
Insted of using device.platform
, you can use cordova.platformId
. 如果使用device.platform
,可以使用cordova.platformId
。 I'm sure it will help 我相信它会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.