如何创建一个“第一负载”事件在html或javascript? [英] How can I create a "first-load" event in html or javascript?

查看:152
本文介绍了如何创建一个“第一负载”事件在html或javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者。我有个主意。我想实现这样的事件。可以吗?

 < html> 
< head>
< title>首次访问事件< / title>

< script>
函数do_something()
{
alert(你好我的朋友,这是你的第一次访问);
}
< / script>

< / head>

< body firstVisitEvent =do_something()>
< / body>


解决方案

在加载时使用。它将在页面加载时执行,您将看到您的警报。祝你有所了解。

 < html> 
< head>
< script>
function load()
{
alert(Page is loaded);
}
< / script>
< / head>
< body onload =load()>
< h1> Hello World!< / h1>
< / body>
< / html>

编辑:您可以获得更多提前和设置Cookie,看看它是用户的第一次,但是不知道你在那个级别,但无论如何:



你可以设置一个localStorage属性,如


$ b $如果(!localStorage [alertdisplayed]){
alert(Your text)
localStorage [alertdisplayed] = true
}

请注意,旧版浏览器不支持localStorage。 / p>

另一种方法是设置Cookie并检查其存在



如果您不知道如何设置/ javascript获取cookie,我建议,阅读这篇文章: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie?redirectlocale=en-US&redirectslug=DOM%2Fdocument.cookie


I'm starter. I have an idea. I want to implement an event like this. Is it possible?

<html>
<head>
<title>First visit event</title>

<script>
function do_something()
{
    alert("Hello my friend. This is your first visit.");
}
</script>

</head>

<body firstVisitEvent="do_something()">
</body>

解决方案

Use on load. It will execute when the page loads, and you will see your alert. Good luck with your knowledge.

    <html>
    <head>
    <script>
    function load()
    {
    alert("Page is loaded");
    }
    </script>
    </head>
    <body onload="load()">
    <h1>Hello World!</h1>
    </body>
    </html> 

Edit: You can get more advance and set cookies to see if it's the user's first time, but I'm not sure you're at that level yet, but anyways:

You could for example set a localStorage property like

if(!localStorage["alertdisplayed"]) {
    alert("Your text")
    localStorage["alertdisplayed"] = true
}

Note that localStorage isn't supported by older Browsers.

An alternative would be setting a Cookie and check against its existence

If you don't know how to set / get cookies with javascript, i would suggest, reading this article: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie?redirectlocale=en-US&redirectslug=DOM%2Fdocument.cookie

这篇关于如何创建一个“第一负载”事件在html或javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆