为什么我的jquery饼干不可用跨多个页面? [英] why are my jquery cookies not available across multiple pages?

查看:109
本文介绍了为什么我的jquery饼干不可用跨多个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本周,我研究并把一些jquery(这是我第一次使用jquery)在一个自定义的WordPress sidebar.php文件中控制一个可折叠菜单。动态生成菜单,使用wp_query通过自定义分类和自定义排序顺序等选择和排序自定义的职位类型(因此没有插件解决方案)。

This week I have researched and put together some jquery (it's my first time working with jquery) to control a collapsible menu in a custom WordPress sidebar.php file. The menu is dynamically generated, using wp_query to select and sort custom post types by custom taxonomies and custom sort orders, etc. (so there is no plugin solution).

我想要在网站访问者浏览内容时维护我的菜单部分的展开/折叠状态。使用jquery它似乎我成功地写入唯一的cookie名称到浏览器,它似乎我成功地通过我的标签的扩展或折叠类值。

I want to maintain the expand/collapse state of my menu sections as site visitors navigate the content. Using jquery it appears I am successfully writing unique cookie names to the browser, and it appears I am successfully passing the "expanded" or "collapsed" class values of my tags.

但是,Cookie不能在多个页面上工作 - 如果您导航到另一个页面,则在单​​击菜单项之前不会设置新的Cookie。如果您点击后退浏览器并返回上一页,上一页设置的Cookie仍仍然设置(这是好的)。注意,当导航到新页面时,菜单将重新生成,但是菜单内容很少更改(因此ID是稳定的),并且cookie仅用于当前会话(并且仍应保留在上一页) 。

However, the cookies don't work across multiple pages - if you navigate to another page, no new cookies are set until you click a menu item. If you hit the back browser and return to the previous page, your cookies set on the previous page are still set (which is good). Note that when navigating to a new page the menu DOES get regenerated, but the menu content rarely changes (so IDs are stable) and the cookies are only for the current session (and should still be held over from the previous page(s)).

我已经尝试设置PHP cookie,并且他们从一个页面到另一个页面保持得很好。但是我的jQuery cookie似乎是页面特定的,所以我做错了。

I've experimented with setting PHP cookies, and they hold up nicely from page to page. But my jquery cookies seem to be page-specific, so I'm doing something wrong.

这里是jquery代码出现在我的PHP页面来控制菜单和写我的Cookie(注意:我 链接到jquery.cookie.js插件):

Here's the jquery code that appears in my PHP page to control the menu and write my cookies (NOTE: I am linking to the jquery.cookie.js plugin):

        <script type="text/javascript">
        $(document).ready(function() {
            setTimeout(function() {
                $('#port-menu > li > a.expanded + ul, #port-menu > li ul li > a.collapsed + ul').slideToggle('medium');
                $('#port-menu > li > a').click(function() {
                    $(this).toggleClass('expanded').toggleClass('collapsed').parent().find('> ul').slideToggle('medium');
                    var vartag = $(this).get(0).tagName.toLowerCase();
                    var varclass = $(this).attr('class');
                    var cookiename = $(this).parent().attr('id');
                    $.cookie(cookiename, $(this).attr('class'));
                });
                $('#port-menu > li ul li > a').click(function() {
                    $(this).toggleClass('expanded').toggleClass('collapsed').parent().find('> ul').slideToggle('medium');
                    var vartag = $(this).get(0).tagName.toLowerCase();
                    var varclass = $(this).attr('class');
                    var vargenre = $(this).parent().parent().parent().attr('id');
                    var varoutlet = $(this).parent().attr('id');
                    var cookiename = vargenre + varoutlet;
                    $.cookie(cookiename, $(this).attr('class'));
                });
            }, 250);
        });
    </script>


$ b < b
$ b

As an example, the dynamically created cookie names and values look something like this:


genre2:折叠

genre2 : collapsed

genre3:展开

genre1outlet2:展开

genre1outlet2 : expanded

genre2outlet3:展开

genre2outlet3 : expanded

genre2outlet4:展开

genre2outlet4 : expanded

如上所述,我是jquery的新手。我也是新使用cookies。任何帮助将非常感激。

As mentioned above, I'm new to jquery. I'm also new to using cookies. Any help would be greatly appreciated.

一旦我得到这个工作,我不知道如何从我的数组的饼干信息,并应用这些类到我的菜单 - 但这是一个单独的问题。

P.S. Once I get this working, I have no idea how to get the info from my array of cookies and apply the classes to my menu - but that's a separate issue.

推荐答案

我认为您应该尝试指定路径。它可能如下所示:

I think you should try to specify a path. It could look like this:

$.cookie(cookiename, $(this).attr('class'), { path: '/' });

然后当您设置cookie一次,它将可用于所有网站。默认路径是设置cookie的子页面的路径。在这种情况下,Cookie仅适用于该子页面。

Then when you set cookie once it will be availabe for all website. Default path is the path of subpage which set cookie. In that case cookie is available only for that subpage.

此规则(我的意思是路径)一般适用于Cookie。 不仅由jQuery创建的。

This rule (I mean with paths) applies to cookies in general. Not only those created by jQuery.

这篇关于为什么我的jquery饼干不可用跨多个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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