Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript只显示一次弹出消息,但每次加载页面时都会显示_Javascript_Jquery_Html_Popup - Fatal编程技术网

Javascript只显示一次弹出消息,但每次加载页面时都会显示

Javascript只显示一次弹出消息,但每次加载页面时都会显示,javascript,jquery,html,popup,Javascript,Jquery,Html,Popup,我试图做的是当用户只访问一次页面时,弹出窗口显示给用户 但它显示给我每次我刷新页面,而且我不需要它提醒我需要它弹出窗口 请告知 //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 alertmessage="Wel

我试图做的是当用户只访问一次页面时,弹出窗口显示给用户 但它显示给我每次我刷新页面,而且我不需要它提醒我需要它弹出窗口

请告知

//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 alertmessage="Welcome to JavaScript Kit! \n http://javascriptkit.com"

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

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

//警报消息一次脚本-通过JavaScript工具包
//信用通知必须保持完整,以便使用
//拜访http://javascriptkit.com 对于这个脚本
//指定要发出警报的消息
var alertmessage=“欢迎使用JavaScript工具包!\nhttp://javascriptkit.com"
///在此之外无需编辑/////
//每个浏览器会话仅发出一次警报(0=否,1=是)
每会话一次var=1
函数get_cookie(名称){
变量搜索=名称+“=”
var returnvalue=“”;
如果(document.cookie.length>0){
偏移量=document.cookie.indexOf(搜索)
if(offset!=-1){//if cookie存在
偏移量+=搜索长度
//设置值的起始索引
end=document.cookie.indexOf(“;”,offset);
//设置cookie值结尾的索引
如果(结束==-1)
end=document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset,end))
}
}
返回值;
}
函数alertornot(){
如果(获取cookie('alerted')=''){
loadalert()
document.cookie=“alerted=yes”
}
}
函数loadalert(){
警报(警报消息)
}
if(每会话一次==0)
loadalert()
其他的
alertornot()
如何运行代码? 如果您正在本地计算机上以文件形式在浏览器中运行,例如

file://localhost/../../../file.html
您无法从javascript保存cookie

您需要在Web服务器上运行代码,并使用http通过该服务器访问代码

http://localhost/../../../file.html
w3schools有一些非常好的例子:

e、 g


为什么不使用本地存储呢?我需要使用cookie来实现这一点。
function setCookie(cname,cvalue,exdays) {
 var d = new Date();
 d.setTime(d.getTime()+(exdays*24*60*60*1000));
 var expires = "expires="+d.toGMTString();
 document.cookie = cname + "=" + cvalue + "; " + expires;
}