如何在html或javascript中创建“首次加载”事件?

时间:2013-10-20 05:20:31

标签: javascript html events javascript-events visited

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

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

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

Localstorage可能是要走的路。以下是一些信息: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

答案 2 :(得分:1)

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

以下是HTML5网络存储入门的链接:

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

答案 3 :(得分: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>

答案 4 :(得分:0)

由于操作符和函数名称的原因,使用localStorage可能很繁琐,为简化起见,可以使用in运算符检查键是否存在:

import sys

class TextsObjects:
    def __init__(self):
        self.setting = Settings()
        self.intro_settings = IntroSettings()

        self.display_intro_1 = DisplayText(
            self.intro_settings.intro_text_1,
            self.intro_settings.intro_text_1_pos_x,
            self.intro_settings.intro_text_1_pos_y,
            self.intro_settings.font_size,
            self.setting.light_grey
            )

        self.display_intro_2 = DisplayText(
            self.intro_settings.intro_text_2,
            self.intro_settings.intro_text_2_pos_x,
            self.intro_settings.intro_text_2_pos_y,
            self.intro_settings.font_size,
            self.setting.light_grey
            )

        self.display_intro_title = DisplayText(
            self.intro_settings.intro_text_title,
            self.intro_settings.intro_text_title_pos_x,
            self.intro_settings.intro_text_title_pos_y,
            self.intro_settings.font_size,
            self.setting.light_grey)


class Intro:
    def __init__(self, screen, setting):
        self.screen = screen
        self.setting = setting
        self.i_setting = IntroSettings()
        self.intro_text = TextsObjects()
        self.clock = pygame.time.Clock()
        self.speed = 150 * setting.time
        print(self.speed)
        self.screen_c_left = setting.screen_width / 2 - self.speed / 2
        self.screen_c_right = setting.screen_width / 2 + self.speed / 2

    def intro_update(self):
        # reset the background so you don't get multiple drawings onscreen
        self.screen.fill(self.setting.dark_blueish)

        if 0 <= self.i_setting.intro_text_1_pos_x <= self.setting.screen_width:

            # Draw the first text
            self.intro_text.display_intro_1.draw_me_with_shadow(self.screen)

        if self.screen_c_left < self.i_setting.intro_text_2_pos_x < self.screen_c_right:

            # Draw the second text
            self.intro_text.display_intro_2.draw_me_with_shadow(self.screen)

        pygame.display.update()

    def show_intro(self):
        #self.i_setting.intro_text_1_pos_x += self.speed

        # show the intro for self.intro_time amount of time
        for frames in range(0, self.setting.fps * 3):
            # test for events
            for event in pygame.event.get():
                print(event)
                # when the window is closed stop the game loop
                if event.type == pygame.QUIT:
                    sys.exit()

            #self.i_setting.intro_text_1_pos_x += self.speed
            self.intro_text.display_intro_1.text_rect.x += self.speed
            self.intro_text.display_intro_1.text_rect_shadow.x += self.speed

            self.intro_update()
            self.setting.clock.tick(self.setting.fps)


class Settings:
    def __init__(self):

        # screen size
        self.screen_width = 800
        self.screen_height = 500

        # set clock and fps
        self.clock = pygame.time.Clock()
        self.fps = 60
        # calculate the seconds in every frame
        self.time = 1 / self.fps

        self.light_grey = (128,128,128)
        self.dark_blueish = (0,0,255)

class IntroSettings:
    def __init__(self):
        self.setting = Settings()
        self.font_size = 100
        self.timer_font_size = 10

        self.intro_text_1_pos_x = (self.setting.screen_width / 2) - 100
        self.intro_text_1_pos_y = \
            (self.setting.screen_height / 2 - self.font_size)
        self.intro_text_1 = "A game by:"

        self.intro_text_2_pos_x = (self.setting.screen_width / 2)
        self.intro_text_2_pos_y = \
            (self.setting.screen_height / 2 + self.font_size)
        self.intro_text_2 = "Mark Olieman"

        self.intro_text_title_pos_x = (self.setting.screen_width / 2)
        self.intro_text_title_pos_y = (self.setting.screen_height + self.font_size)
        self.intro_text_title = "Alien Invasion!"

        self.intro_text_timer_pos_x = 20
        self.intro_text_timer_pos_y = 20

        self.speed = -2


class DisplayText:
    def __init__(self, text, pos_x, pos_y, font_size, text_color):

        self.font = pygame.font.Font("freesansbold.ttf", font_size)

        self.text_surf = self.font.render(text, True, text_color)
        self.text_rect = self.text_surf.get_rect()
        self.text_rect.center = (pos_x, pos_y)

        self.shadow_color = (0, 0, 0)

        self.text_surf_shadow = self.font.render(text, True, self.shadow_color)
        self.text_rect_shadow = self.text_surf.get_rect()
        self.text_rect_shadow.center = (pos_x + 10, pos_y + 5)

    def draw_me(self, screen):
        screen.blit(self.text_surf, self.text_rect)

    def draw_me_with_shadow(self, screen):
        screen.blit(self.text_surf_shadow, self.text_rect_shadow)
        screen.blit(self.text_surf, self.text_rect)

import pygame
#from Entities import *
#from Game_functions import *
#from Menu import *

# initizialize pygame
pygame.init()
pygame.mixer.init()

# make the modules available
setting = Settings()

screen = pygame.display.set_mode((setting.screen_width, setting.screen_height))
pygame.display.set_caption("Alien Invasion, By Mark Olieman")

intro = Intro(screen, setting)
#player = Player(screen, setting)
#bullet = Bullet(screen, setting, player)
intro_set = IntroSettings()

# initizialize the screen and set a caption


# function to start gaming
def gaming():
    bullets = pygame.sprite.Group()
    intro.show_intro()
    intro_set.intro_text_1_pos_x += intro.speed
    print(intro_set.intro_text_1_pos_x)
    # Game Loop
    while True:
        # check events and make actions happen
        #events(bullets, player, screen, setting)

        # update everything and draw bullets
        #update(player, setting, bullets, screen)

        # update screen at certain fps
        pygame.display.update()
        setting.clock.tick(setting.fps)

# Start the game
gaming()

# Stop the game
pygame.quit()