简体   繁体   English

将cookie设置为javascript函数

[英]Set cookie to a javascript function

I have this function : 我有这个功能:

<script type="text/javascript">
$(function () {
    YAHOO.MediaPlayer.addTracks(document.getElementById("ajazload"), 1, false)
});
</scr‌​ipt>

And I'd like to add to this function a cookie, in order to prevent it to reload a second time when the user reloads the page. 并且我想向此函数添加一个cookie,以防止在用户重新加载页面时第二次重新加载它。

May you have an idea to do that ? 你可能有个主意吗?

Working with cookies in jQuery is very simple if you use something like the jQuery.cookie plugin . 如果您使用类似jQuery.cookie插件的方法,则在jQuery中使用Cookie的工作非常简单。

Example code: 示例代码:

if(!$.cookie('mediaplayer')) {
    YAHOO.MediaPlayer.addTracks(document.getElementById("ajazload"), 1, false);
    $.cookie('mediaplayer',1);
}

Use the jQuery Cookie plugin: https://github.com/carhartl/jquery-cookie 使用jQuery Cookie插件: https : //github.com/carhartl/jquery-cookie

$.cookie('site_reloaded', 'true');

Then before you reload the page: 然后,在重新加载页面之前:

if ($.cookie('site_reloaded') != 'true') { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM