每个会话出现一次javascript ok / cancel对话框

时间:2010-05-19 15:21:22

标签: javascript alert

我试图在我的不在MobileMe上的iweb页面中使用它。使用下面的代码,我会在每次刷新页面时继续获取警报框,而不是每个会话一次。我在这里是一个新手,所以请你好心。

//Alert message once script- By JavaScript Kit
//Credit notice must stay intact for use
//Visit http://javascriptkit.com for this script

//specify message to alert
var answer=confirm("Click OK if you have been cleared Sutter's HR department to start       
volunteering.")
if (answer)
 alert ("Excellent!!  Please select your dates directly within the scheduling calendar.")
else
 alert ("Let's get to it then. Contact Ruth in HR at 576-4208 to schedule an appointment     so you can get started.")


///No editing required beyond here/////

//answer only once per browser session (0=no, 1=yes)
var once_per_session=1


function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { // if cookie exists
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1)
         end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function alertornot(){
if (get_cookie('alerted')==''){
loadalert()
document.cookie="alerted=yes"
}
}

function loadalert(){
alert(alertmessage)
}

if (once_per_session==0)
loadalert()
else
alertornot()

</script>

1 个答案:

答案 0 :(得分:2)

您的代码每次会话调用一次:

alert(alertmessage)

但是每次加载脚本时都会调用top上的代码。

此外 - 我没有看到alertmessage定义在哪里...... 所以您可能希望将代码从顶部放在loadalert函数中,从而得到:

function loadalert(){
var answer=confirm("Click OK if you have been cleared Sutter's HR department to start  volunteering.")
if (answer)
 alert ("Excellent!!  Please select your dates directly within the scheduling calendar.")
else
 alert ("Let's get to it then. Contact Ruth in HR at 576-4208 to schedule an appointment     so you can get started.")

}

编辑:

而BTW - 开始使用花括号。它有助于调试和了解您的位置。 :)