如何使警报提示只显示一次?

时间:2013-09-24 18:39:24

标签: javascript html

我只想要警报一次。

<head>
    <script type="text/javascript">
    var name;
    name = window.prompt("Enter name");
    </script>
</head>

这是在身体

<div style="left:750px;top:60px;position:absolute;">
    <script type="text/javascript">
        document.writeln("Welcome "+name+" !");  //name from the request will be display here
    </script>
</div>

=============================================== ===================================

也适用于页面加载时弹出的促销

这里是promotion.html

<body>
<script type="text/javascript">
 PopupCenter()  //for name to be enter on request form
</script>
</body>

这里是promotion.html: -

<body>


     <div id="promotion">
     <table border="1">     
            <tr><td><p>
            <script language="JavaScript">
                <!--
                promo = new Array
                promo[1]="PROMOTION!!! save up to 50% if you buy more than $300"
                promo[2]="Open every monday to saturday!!!! from 9 a.m to 10 p.m"
                promo[3]="The 100th customer will win free reel & spool!!"
                promo[4]="Labour sale day 20% off!! Labour sale day 20% off!!"
                promo[5]="Custom parts and reels bearing will be 10% off!!!"
                promo[6]="Come NOW!! HURRY!! while promotion still available!!"
                promo[7]="Shimano from $230, NOWW ONLY $98!!!"

                random_num = (Math.round((Math.random()*6)+1))

                document.write(promo[random_num]);
                -->
                </script>
            </p></td></tr>           
     </table>
     </div>



    </body>
此.js文件中的

适用于promotion.html

 function PopupCenter(pageURL, title,w,h) {
        var left = (screen.width/2)-(w/2);
        var top = (screen.height/2)-(h/2);
        var targetWin = window.open ("promotion.html", "PROMOTION", 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width='+580+', height='+485+', top='+100+', left='+400);

        } 
        return sessionStorage.Popupccenter=1;

2 个答案:

答案 0 :(得分:2)

sessionStorage中保留一份副本,检查是否存在然后询问是否存在

function getName() {
    var n, q = 'Enter name';
    if (!window.sessionStorage) {  // if sessionStorage not supported
        return window.prompt(q); // perform other action
    }
    n = window.sessionStorage.getItem('name');
    if (!n) {
        n = window.prompt(q);
        window.sessionStorage.setItem('name', n);
    }
    return n;
}

答案 1 :(得分:0)

在提供姓名后设置一个cookie

然后在每次加载页面时检查cookie是否设置

如果未设置,则显示提示,否则不显示