如何创建“& first-load&"HTML或Javascript事件? [英] How can I create a "first-load" event in html or Javascript?

查看:65
本文介绍了如何创建“& first-load&"HTML或Javascript事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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> 

您可以获取更多信息并设置cookie来查看这是否是用户的第一次,但是我不确定您是否处于该级别,但是无论如何:

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:

例如,您可以设置一个localStorage属性,例如

You could for example set a localStorage property like

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

请注意,旧版浏览器不支持localStorage.

另一种选择是设置Cookie并检查其存在性

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

如果您不知道如何使用javascript设置/获取cookie,我建议阅读此文章:

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

这篇关于如何创建“&amp; first-load&amp;"HTML或Javascript事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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