-2

my page is not working with cookies using javascript and url redirecting

if userid/username(cookies) already in my browser, it comes thank for vote,otherwise it goes to login page..what is the problem in below code

<script type="text/javascript">
    var userid = document.cookie;
    var username = document.cookie;
    if (userid != null && username != null)
    {
        $('.review-footer-new').find('.review-feedbackLink').click(function (){
            if ($(this).closest("a").is("[disabled]")) {
                 return false;
            }
            $(this).addClass("selected");
            $(this).parent().after("<span class='thanks'> Thanks for your vote! </span>");
            $(this).closest(".review-feedbackQuestion").find(".review-feedbackLink").closest("a").attr("disabled", "disabled");
        });
    }
    else
    {
        window.location = "http://localhost/login.aspx";
    }
</script>
Manik Arora
  • 4,702
  • 1
  • 25
  • 48
Prakash J
  • 15
  • 8
  • what is it happening now, always showing the message or always going to login page? – Manik Arora Apr 28 '15 at 10:31
  • when new user come to my page ,and comment for any thing for post only for registered user.in case not login my page ,go to login page – Prakash J Apr 28 '15 at 10:36
  • i did thank for vote.bt not it wrking to go login page for not registered users – Prakash J Apr 28 '15 at 10:38
  • Firstly, it was not me voting your question down, but now I am doing it, I think you should first learn some english before asking some question, can't understand your english what you meant in the above two comments, sorry! – Manik Arora Apr 28 '15 at 10:43
  • ok . i need to validate username/userid from cookies in my browser – Prakash J Apr 28 '15 at 10:54
  • then if cookies(userid) is not there ,go to another login page – Prakash J Apr 28 '15 at 10:56
  • I'm not sure but it seems safer to check if `document.cookie` is `falsy`, not if it's `null` as thats not necessarily true (`undefined != null`). Try `if (!document.cookie){}` instead of assigning username and userid the same value and then checking for `null`. – somethinghere Apr 28 '15 at 11:04

1 Answers1

0

Instead of reinventing the wheel with cookies, use already existing libraries to write and read cookies, as they will return you correct and predictable variables. I'd suggest looking at this question:

How do I create and read a value from cookie?

The reason I think it's your cookie is that my cookie in Safari seems to never be empty, as it creates some sort of cookie for me anyhow. In that case, checking for falsy, empty or even null will never work as there's always something there, even if it's not what you wanted.

As for the rest of your code... It is very much reliant on knowing the HTML to see if you are doing it right. There don't seem to be syntax errors, but if jQuery can't find an object and you are trying to manipulate it, it will throw an error, so check your console.

Community
  • 1
  • 1
somethinghere
  • 16,311
  • 2
  • 28
  • 42