开发者

How to display this JavaScript message in my webOS app?

开发者 https://www.devze.com 2023-03-12 04:01 出处:网络
I am writing a webOS app and I want the following code to present a \"Good Morning!\" message. What code do I need to put in my Main-scene.html file and what else do I need in the Main-assistant.js fi

I am writing a webOS app and I want the following code to present a "Good Morning!" message. What code do I need to put in my Main-scene.html file and what else do I need in the Main-assistant.js file to make this work?

Many thanks in advance.

MainAssistant.prototype.welcomeMessage = function(){
datetoday = new Date();
timenow = datetoday.getTime();
datetoday.setTime(timenow);
thehour = datetoday.getHours();
if (thehour > 18) 
    dis开发者_运维知识库play = "Evening";
else 
    if (thehour > 12) 
        display = "Afternoon";
    else 
        display = "Morning";
var greeting = ("Good " + display + "!");
document.write(greeting);
};

All the best

David


I don't know when the welcomeMessage method will be called, but if it's after the document has finished loading then it is closed. Calling document.write then will call document.open, which completely clears the document. Likely you have an element that the message should be written into, much better to use the load or DOMReady events (or whatever is available on WebOS).

Consider a much simpler form of the function and don't forget to declare variables to limit their scope to only what is necessary:

  var tod = ['morning','morning','afternoon','evening'];
  var now = new Date();
  var greeting = 'Good ' + (tod[now.getHours()/6|0]) + '!';

If you want a library, try myLibrary. No, it's not my Library, it's your library when you use it. You can create a customised core and just the DOM ready part that is quite small and the code quality is excellent.

0

精彩评论

暂无评论...
验证码 换一张
取 消