javascript在cordova应用中不起作用

编程入门 行业动态 更新时间:2024-10-24 14:19:41
本文介绍了javascript在cordova应用中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

JavaScript在我的cordova应用程序中似乎无法正常运行,而且我似乎无法弄清楚我在这里做错了什么.我肯定知道在普通的Web浏览器中html和javascript都可以正常工作.

Javascript doesn't seem to be working in my cordova app, and I don't seem to be able to figure out what I'm doing wrong here. I know for sure in a normal webbrowser the html and javascript works fine.

下面是我的代码,我正在创建一个简单的计算器.

Below is my code, I'm creating a simple calculator.

index.html:

index.html:

<!DOCTYPE html> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at www.apache/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <html> <head> <!-- Customize this policy to fit your own app's needs. For more guidance, see: github/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy Some notes: * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication * ssl.gstatic is required only on Android and is needed for TalkBack to function properly * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this: * Enable inline JS: add 'unsafe-inline' to default-src --> <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: ssl.gstatic 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;"> <meta name="format-detection" content="telephone=no"> <meta name="msapplication-tap-highlight" content="no"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> <link rel="stylesheet" type="text/css" href="css/design.css"> <title>Calculator</title> </head> <body> <div id="calculator"> <form> <input type="text" id="display" disabled><br> <input type="button" value="C" id="keys" onclick="addtoscreen('c')"> <input type="button" value="<--" id="keys" onclick="backspace()"> <input type="button" value="X^2" id="keys" onclick="power(2)"> <input type="button" value="+" id="keys" onclick="addtoscreen('+')"><br> <input type="button" value="9" id="keys" onclick="addtoscreen('9')"> <input type="button" value="8" id="keys" onclick="addtoscreen('8')"> <input type="button" value="7" id="keys" onclick="addtoscreen('7')"> <input type="button" value="-" id="keys" onclick="addtoscreen('-')"><br> <input type="button" value="6" id="keys" onclick="addtoscreen('6')"> <input type="button" value="5" id="keys" onclick="addtoscreen('5')"> <input type="button" value="4" id="keys" onclick="addtoscreen('4')"> <input type="button" value="*" id="keys" onclick="addtoscreen('*')"><br> <input type="button" value="3" id="keys" onclick="addtoscreen('3')"> <input type="button" value="2" id="keys" onclick="addtoscreen('2')"> <input type="button" value="1" id="keys" onclick="addtoscreen('1')"> <input type="button" value="/" id="keys" onclick="addtoscreen('/')"><br> <input type="button" value="0" id="keys" onclick="addtoscreen('0')"> <input type="button" value="." id="keys" onclick="addtoscreen('.')"> <input type="button" value="=" id="equal" onclick="answer()"> </form> </div> </body> <script type="text/javascript" src="js/logic.js"> </script> </html>

logic.js:

var box = document.getElementById("display"); function addtoscreen(x){ box.value +=x; if(x == "c"){ box.value=''; } } function answer(){ x=box.value x=eval(x); box.value=x; } function backspace(){ var number=box.value; var len= number.length-1; var newnumber=number.substring(0, len) box.value=newnumber; } function power(y){ x=box.value; x=Math.pow(x, y); box.value=x; }

推荐答案

对于其他有相同问题的人,我添加了这一行来解决.

For other people having the same problem, I fixed it by adding this line.

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

更多信息可以在这里找到: cordova.apache/docs/en/latest/reference/cordova-plugin-whitelist/

more information can be found here: cordova.apache/docs/en/latest/reference/cordova-plugin-whitelist/

更多推荐

javascript在cordova应用中不起作用

本文发布于:2023-11-27 21:16:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639632.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   javascript   cordova

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!