2
<html>
  <head>
    <meta name="viewport" id="viewport" content="height=device-height,width=device-width,user-scalable=no"/>
    <script type="text/javascript">
    function helloWorld() {
     alert("Hello World");
    }
    </script>
  </head>
  <body onload="helloWorld();">
   <h1>Hello World</h1>
  </body>
</html>

我有一个使用类似于上面的 Blackberry WebWorks 构建的应用程序。每次用户打开应用程序时,我都需要触发上述 helloWorld() 函数。

问题是“onload”功能仅在应用程序首次启动时触发,或者当用户通过单击“手机上的挂断按钮”退出应用程序时,而不是在单击“手机上的后退按钮”时触发。

有什么建议吗?

4

1 回答 1

1

I think that you are interested in launching the function not only every time the app is started, but also when the app is retrieved from the background (this means when your app is not closed, but it is running in the background although you are not interacting with it).

I suggest you use Cordova (former Phonegap) and take a look at the "resume" event. Using the example given there, I think you would need something like this:

<html>
<head>
  <title>Cordova Resume Example</title>

  <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
  <script type="text/javascript" charset="utf-8">

  // Call onDeviceReady when Cordova is loaded.
  // At this point, the document has loaded but cordova-1.7.0.js has not.
  // When Cordova is loaded and talking with the native device,
  // it will call the event `deviceready`.
  function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
  }

  // Cordova is loaded and it is now safe to make calls Cordova methods
  function onDeviceReady() {
      document.addEventListener("resume", onResume, false);
      // Call the function you are interested in.
      helloWorld();
  }

  // Handle the resume event
  function onResume() {
    helloWorld();
  }

  function helloWorld(){
    alert('Hello World');
  }
  </script>
</head>
<body onload="onLoad()">

</body>
</html>

You may download the files you need here. I haven't test the code. Try it and let me know if it works for you.

于 2012-05-18T21:53:57.693 回答