如何解决“输入意外结束”错误

时间:2019-04-09 17:52:59

标签: javascript html5

我已经看过其他几个与此错误有关的问题,但其中的任何解决方案均无效。我使用的是教科书,它让我从网站复制代码并将其粘贴到script标签内(这并不是说将其粘贴到script标签内,只是说将其粘贴到HTML文档中)。在以下代码的结束脚本标记处,我始终收到“语法错误:输入意外结束”的信息:

<script>
            (function () {
                "use strict";

                var app = WinJS.Application;
                var activation = Windows.ApplicationModel.Activation;

                app.onactivated = function (args) {
                    if (args.detail.kind === activation.ActivationKind.launch) {

                        args.setPromise(WinJS.UI.processAll().
                            done(function () {

                            }));
                    }
                };
            (function () {
                "use strict";

                var app = WinJS.Application;
                var activation = Windows.ApplicationModel.Activation;

                app.onactivated = function (args) {
                    if (args.detail.kind === activation.ActivationKind.launch) {

                        args.setPromise(WinJS.UI.processAll().
                        done(function () {

                        // Add an event handler to the button.
                        document.querySelector("#requestPosition").addEventListener("click",
                            requestPosition);

                        }));
                    }
                };

                var nav = null;

                function requestPosition() {
                    if (nav == null) {
                        nav = window.navigator;
                    }

                    var geoloc = nav.geolocation;
                    if (geoloc != null) {
                        geoloc.getCurrentPosition(successCallback, errorCallback);
                    }

                }

                function successCallback(position) {
                    document.getElementById("latitude").innerHTML =
                        position.coords.latitude;
                    document.getElementById("longitude").innerHTML =
                        position.coords.longitude;

                }

                function errorCallback(error) {
                    var strMessage = "";

                    // Check for known errors
                    switch (error.code) {
                        case error.PERMISSION_DENIED:
                            strMessage = "Access to your location is turned off. " +
                                "Change your settings to turn it back on.";
                            break;
                        case error.POSITION_UNAVAILABLE:
                            strMessage = "Data from location services is " +
                                "currently unavailable.";
                            break;
                        case error.TIMEOUT:
                            strMessage = "Location could not be determined " +
                                "within a specified timeout period.";
                            break;
                        default:
                            break;
                    }

                    document.getElementById("status").innerHTML = strMessage;
                }

                app.start();
            }()
        </script>

这是在最后一章中,只是练习的第一部分,因此,我们将不胜感激。我以前尝试过的事情包括在JavaScript末尾添加另一个括号,但这导致了另一个错误。如果有帮助,请参见MTA HTML5工作簿的第10课

1 个答案:

答案 0 :(得分:0)

您重复了javascript代码的第一部分,并且缺少了“)”,从而关闭了函数声明;

(function() {
"use strict";

var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;

app.onactivated = function(args) {
  if (args.detail.kind === activation.ActivationKind.launch) {
    args.setPromise(
      WinJS.UI.processAll().done(function() {
        // Add an event handler to the button.
        document
          .querySelector("#requestPosition")
          .addEventListener("click", requestPosition);
      })
    );
  }
};

var nav = null;

function requestPosition() {
  if (nav == null) {
    nav = window.navigator;
  }

  var geoloc = nav.geolocation;
  if (geoloc != null) {
    geoloc.getCurrentPosition(successCallback, errorCallback);
  }
}

function successCallback(position) {
  document.getElementById("latitude").innerHTML = position.coords.latitude;
  document.getElementById("longitude").innerHTML = position.coords.longitude;
}

function errorCallback(error) {
  var strMessage = "";

  // Check for known errors
  switch (error.code) {
  case error.PERMISSION_DENIED:
    strMessage =
      "Access to your location is turned off. " +
      "Change your settings to turn it back on.";
    break;
  case error.POSITION_UNAVAILABLE:
    strMessage =
      "Data from location services is " + "currently unavailable.";
    break;
  case error.TIMEOUT:
    strMessage =
      "Location could not be determined " +
      "within a specified timeout period.";
    break;
  default:
    break;
}

document.getElementById("status").innerHTML = strMessage;
}

app.start();
})();