简体   繁体   中英

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. 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. 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. When the user reaches the homepage, the homepage checks that cookie and scrolls the to the proper area.

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. 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). Since here you are talking about creating cookies in JavaScript, the Http only cookies are non existent.
This javascript basic library will give you the tools to do what you want.

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). This javascript library will explain how to use it.

Hope I helped, Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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