2

我是首发。我有个主意。我想实现这样的事件。可能吗?

<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>
4

5 回答 5

2

负载使用。它将在页面加载时执行,您将看到您的警报。祝你好运。

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

编辑:您可以更进一步并设置 cookie 以查看是否是用户的第一次,但我不确定您是否处于该级别,但无论如何:

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

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

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

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

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

于 2013-10-20T05:24:01.367 回答
2

本地存储可能是要走的路。这里有一些关于它的信息:http: //diveintohtml5.info/storage.html

基本上,它允许您在用户的机器上存储少量数据(在大多数浏览器中最多为 5mb)。因此,您需要做的就是在其中设置一个标志,让您知道用户已经访问过,如果该标志在页面加载时不存在,则显示该消息。

// Run function on load, could also run on dom ready
window.onload = function() {
    // Check if localStorage is available (IE8+) and make sure that the visited flag is not already set.
    if(typeof window.localStorage !== "undefined" && !localStorage.getItem('visited')) {
         // Set visited flag in local storage
         localStorage.setItem('visited', true);
         // Alert the user
         alert("Hello my friend. This is your first visit.");   
    }
}

这应该适用于本地存储可用的所有浏览器。请参阅 caniuse.com 以供参考。 http://caniuse.com/#search=localstorage

于 2013-10-20T05:44:08.487 回答
1

您有两个选择:在客户端存储 cookie 或使用 html5 的本地存储。可以在页面加载时检查其中任何一个。

这是一个开始使用 HTML5 网络存储的链接:

http://www.w3schools.com/html/html5_webstorage.asp

于 2013-10-20T05:23:33.200 回答
1

您可以通过以下方式为 chrome 执行此操作

<html>
<head> 
<title>First visit event</title>    
<script> 
function do_something() {
    if(typeof(localStorage.setVisit)=='undefind' || localStorage.setVisit==''){
        localStorage.setVisit='yes';
        alert("Hello my friend. This is your first visit.");
    }
} 
</script>

</head>

<body onload="do_something()"> </body>
于 2013-10-20T05:26:18.567 回答
0

由于运算符和函数名称的存在,使用 localStorage 可能会很乏味,为了简化您可以使用 in 运算符来检查键是否存在:

window.onload = function () {
        if(!('hasThisRunBefore' in localStorage)) {
            /** Your code here... **/
            localStorage.setItem("hasThisRunBefore", true);
        }
    }
于 2020-02-20T19:46:05.587 回答