Jquery会话 - 一次执行脚本?

时间:2012-12-02 21:52:39

标签: jquery

我点击它时会打开/关闭此滑块,但我也是这样,它也会在页面加载时打开,但现在问题是它会在每个页面上打开(因为它出现在每一页上。)

是否有可能以某种方式使其每次会话仅一次自动启动?

2 个答案:

答案 0 :(得分:0)

例如,如果你的意思是一个php会话:

$html = $someHtml;
if (!isset($_SESSION['firstTime'])) {
    $html .= "<script>myJavaScriptFunction();</script>";
    $_SESSION['firstTime'] = "Nope";
}
echo $html;

答案 1 :(得分:0)

您可以使用cookies。

function setCookie(c_name,value,exdays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name){
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++){
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name){
        return unescape(y);
      }
    }
}

if(!getCookie("sliderOpen")){
 //Open your slider
 setCookie("sliderOpen", 1, 1/*time until cookie expires*/ );
}

<小时/> setCookie()getCookie()函数来源:http://www.w3schools.com/js/js_cookies.asp

相关问题