nbalcom Posted May 9, 2013 Share Posted May 9, 2013 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? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 9, 2013 Share Posted May 9, 2013 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. Link to comment Share on other sites More sharing options...
nbalcom Posted May 9, 2013 Author Share Posted May 9, 2013 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? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 9, 2013 Share Posted May 9, 2013 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. Link to comment Share on other sites More sharing options...
nbalcom Posted May 9, 2013 Author Share Posted May 9, 2013 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. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 9, 2013 Share Posted May 9, 2013 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> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now