简体   繁体   English

如何创建onclick cookie链接,然后导航到链接的url之后,让该cookie重定向当前url

[英]How to create an onclick cookie link and then after navigating to link's url, have that cookie redirect the current url

Okay, so this may sound confusing, so let me explain. 好的,这听起来可能令人困惑,所以让我解释一下。 I'm working with a theme in wordpress that has a single page layout and standalone page layouts. 我正在使用一个具有单个页面布局和独立页面布局的wordpress主题。 In the single page layout, every navbar link you click on scrolls you to a section of the page. 在单页面布局中,您单击的每个导航栏链接都将您滚动到页面的一部分。 In the standalone pages, when you click on a navbar link that contains content for the home page, it links you to a standalone page of that content rather than going to the home page and scrolling to the content. 在独立页面中,单击包含主页内容的导航栏链接时,它会将您链接到该内容的独立页面,而不是转到主页并滚动到该内容。

Now before I get many answers saying just do url/#content block, it doesn't work as the theme creator decided to use multiple ids all named content. 现在,在我得到很多答案之前,说只是做url /#content块,这是行不通的,因为主题创建者决定对所有命名的内容使用多个ID。 Horrible I know. 我知道这太可怕了。 I've tried a lot of things actually. 我实际上已经尝试了很多东西。 So the idea I have now, is to store a cookie when the user clicks a link in the #header navbar and store a cookie in the browser. 因此,我现在的想法是在用户单击#header导航栏中的链接时存储cookie,并在浏览器中存储cookie。 When the user reaches the homepage, the homepage checks that cookie and scrolls the to the proper area. 当用户到达主页时,主页会检查该cookie并将其滚动到适当的区域。

I've never worked with cookies to know how to write the code, I just understand how they work from php, I figure javascript is somewhat similar. 我从来没有使用过cookie来了解如何编写代码,我只是了解它们如何从php中工作,我认为javascript有点相似。 If something is unclear, please ask. 如果不清楚,请询问。

You have 2 types of cookies, Http only and regular (you have more, but for this question the others are non relevant). 您有两种类型的cookie,仅Http和常规的cookie(您有更多类型,但是对于这个问题,其他类型都不相关)。 Since here you are talking about creating cookies in JavaScript, the Http only cookies are non existent. 因为这里是在谈论用JavaScript创建cookie,所以不存在仅使用Http的cookie。
This javascript basic library will give you the tools to do what you want. javascript基本库将为您提供执行所需功能的工具。

Now, from my own view of this problem, I would recommend using local storage only if your viewers are using new browsers (old IE won't work). 现在,从我自己对这个问题的观点来看,我建议仅在您的查看器使用新浏览器的情况下才使用本地存储(旧的IE无法使用)。 This javascript library will explain how to use it. 这个javascript库将说明如何使用它。

Hope I helped, Cheers! 希望我能帮助,干杯!

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

相关问题 如何将当前的 URL 存储在 cookie 中,并使用该 cookie 检查其他页面,如果可用,然后重定向到 JS 中的相同 URL? - How to store current URL in cookie, and use that cookie to check in other page, if available then redirect to the same URL in JS? 使用express设置cookie后如何重定向url? - How to redirect url after setting cookie using express? 如何创建将保留当前网址路径的链接 - How to create a link that will keep the current url path Javascript 每小时创建 cookie,如果 cookie 不存在,并且如果 cookie 存在:请重定向到其他 url - Javascript create cookie every hour, if cookie not exists, and if cookie exists : do redirect to other url 在导航到新链接之前先href附加当前URL - href appending current URL before navigating to new link Mailto链接导航到其他URL - Mailto link is navigating to different url 仅当 cookie 存在时如何获取 URL 否则重定向到不同的 url? - How to get URL only if cookie exists else redirect to different url? 将 URl 添加到 cookie 并在返回时重定向到 URL - Add URl to cookie and redirect to URL on return 如何重定向或链接到另一个页面的DIV元素内的URL? - How to Redirect or Link to a URL Inside Another Page's DIV Element? 根据当前 URL 创建指向另一个目录的链接 - Create a link to another directory based on the current URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM