javascript 确定/取消对话框每个会话出现一次

发布于 2024-09-02 15:43:51 字数 1496 浏览 4 评论 0原文

我正在尝试让它在我的 iweb 页面中运行,而不是在 MobileMe 上托管。使用下面的代码,我继续在每次刷新页面时收到警报框,而不是每次会话一次。我是这里的新手,所以请友善。

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

I am trying to get this to work in my iweb page hosted not on MobileMe. With the code below I continue to get the alert box on each refresh of the page instead of once per session. I am a total newbie here so be kind please.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终遇你 2024-09-09 15:43:51

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

alert(alertmessage)

但每次加载脚本时都会调用顶部的代码。

此外 - 我不知道 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.")

}

编辑:

顺便说一句 - 开始使用大括号。它有助于调试和了解您所在的位置。 :)

Your code calls this once per session:

alert(alertmessage)

but the code on top is called on each load of the script.

Moreover - I don't see where alertmessage is defined...
So You probably want to put the code from the top inside the loadalert function resulting in this:

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.")

}

EDIT:

And BTW - start using curly braces. It helps in debug and in understanding where You are. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文