Jump to content
nbalcom

Help writing a cookie activated onclick event for Disclaimer Page

Recommended Posts

Hi everyone. I'm trying to write a cookie for a site I'm working on the displays a page until it triggers an onclick event. I have a section of the site that pulls up a disclaimer page with 2 options one that says I disagree and shoots you to the home page and one that triggers the cookie which if it's started in your browser will redirect you to the section you need to agree to in order to see. I've been able to get it to work upon visit, but I'm not having any luck with the on click event. I've been tweaking the script I found here --> http://www.javascrip...redirect-2.html I feel like I'm pretty close and have tried many onclick variations the following code I think is close. <a href="after-disclaimer-page.com" onClick="SetCookie('HasVisited')">I AGREE</a> Anyone have any ideas?

Share this post


Link to post
Share on other sites

If you're going to require that the cookie gets set, and you're going to do that through Javascript, then you're requiring that they have Javascript enabled. So replace the link with something else (span, etc) that will run the onclick action which sets the cookie and then does the redirection. Clicking on a normal link like that might not have a chance to set the cookie before the browser follows the link. If you do everything in Javascript then you can set the cookie before you tell the browser to go somewhere else.

Share this post


Link to post
Share on other sites

How do I write a span that reacts like a link? I understand what you're saying, but don't quite know how to do this? Do I have to define a class or id in css?

Share this post


Link to post
Share on other sites

Just put an onclick handler on it to do whatever you want when you click on it. If you want to change the color or underline it or change the mouse cursor or whatever you can do that through CSS.

Share this post


Link to post
Share on other sites

Sorry I'm not really a javascript guy. In fact this is the first cookie I've ever attempted to write. I don't appear to be wrapping my head around this. Isn't that an onclick event I have defined? <a href="after-disclaimer-page.com" onClick="SetCookie('HasVisited')">I AGREE</a> Are you saying I should use something like this? <span="after-disclaimer-page.com" onClick="SetCookie('HasVisited')">I AGREE</span> I know that isn't correct, but I don't quite understand how I format a span to react link an href.

Share this post


Link to post
Share on other sites

Just this: <span onClick="click_agree()">I AGREE</span> Then you write a function called click_agree which runs your SetCookie function, and then redirects. You can set window.location.href to redirect the browser:

function click_agree(){  SetCookie('HasVisited');  window.location.href = "http://www.google.com";}

You could also skip the function and put the entire thing inline, although it would be better to use the function in case you want to add anything else later: <span onClick="SetCookie('HasVisited'); window.location.href = 'http://www.google.com';">I AGREE</span>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...