带有cookie的javascript欢迎消息 [英] javascript welcome message with cookie

查看:65
本文介绍了带有cookie的javascript欢迎消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!你可以帮我改进这段代码吗?当点击确定时,设置了cookie但是当点击取消时,没有设置cookie





Hi! Can you help me improve this code in that when "OK" is clicked, cookie is set but when "Cancel" is clicked, cookie is not set


function rC(nam) 
{
var tC = document.cookie.split('; '); 
for (var i = tC.length - 1; i >= 0; i--) 
{
  var x = tC[i].split('=');
  if (nam == x[0]) 
    return unescape(x[1]);
} 
return '~';
} 

function wC(nam,val) 
{
document.cookie = nam + '=' + escape(val);
} 

function lC(nam,pg) 
{
var val = rC(nam); 
if (val.indexOf('~'+pg+'~') != -1) 
  return false; 

val += pg + '~'; 
wC(nam,val); 
return true;
} 

function firstTime(cN) 
{
return lC('pWrD4jBo',cN);
} 

function thisPage() 
{
var page = location.href.substring(location.href.lastIndexOf('\/')+1); 
pos = page.indexOf('.');
if (pos > -1) 
{
page = page.substr(0,pos);
} 
return page;
}

// example code to call it - you may modify this as required
function start() {
   if (firstTime(thisPage())) {
      // this code only runs for first visit
      var answer = confirm ("Please click on OK to continue loading my page, or CANCEL to be directed to the Yahoo site.")
if (!answer)
window.history.back();
   }
   // other code to run every time once page is loaded goes here
}
onload = start;

推荐答案

// example code to call it - you may modify this as required
function start() {
   if (firstTime(thisPage())){
      // this code only runs for first visit
      var answer = confirm ("Please click on OK to continue loading my page, or CANCEL to be directed to the Yahoo site.");
     if(answer)
     {
       lC(nam,pg);// with whatever nam and pg is
     }
     else
     {
       window.history.back();
     }
   }
   // other code to run every time once page is loaded goes here
}


这篇关于带有cookie的javascript欢迎消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆