2017-03-21 99 views
1

Javascript似乎並沒有在我的科爾多瓦應用程序工作,我似乎無法弄清楚我在這裏做錯了什麼。我知道肯定在一個正常的瀏覽器的HTML和JavaScript工作正常。JavaScript不工作在科爾多瓦應用程序

以下是我的代碼,我正在創建一個簡單的計算器。

的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 

    http://www.apache.org/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: 
      https://github.com/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 
      * https://ssl.gstatic.com 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: https://ssl.gstatic.com '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; 
} 

回答

0

我相信這個問題是在你的內容安全,政策meta標籤。您正在防止內聯腳本。下面的代碼應該可以工作。

<meta http-equiv="Content-Security-Policy" content="default-src 'unsafe-inline' data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *"> 

有許多關於你自己的文件中的內容安全政策的詳情;) https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy

+0

非常感謝將看看吧! – stickfigure4