-3

I am newish to jQuery. I am trying to unset a cookie and then go to the next page.

There are quite a few answers on this subject here and elsewhere. I thought this would be very straightforward but about two hours later I know I must be doing something dumb.

Simple button:

<button class="btn btn-success" id="showInfo">Show intro</button>

Tweaked borrowed function that works nicely:

function setCookie(cookieName, cookieValue, daysToKeep)
{
    var d = new Date();
    d.setTime(d.getTime() + (daysToKeep*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cookieName + "=" + cookieValue + ";" + expires + ";path=/";
}

"Simple" function call on button click:

<script>
    $("#showInfo").on("click", function()
    {
        setCookie("button1","",-1 );
        $(location).attr('href',"2.php");
        // window.location.assign("2.php");
        // window.location.replace("2.php");
    })
</script>

I have tried windows replace, assign and href. I have tried jQuery $(location).

I know I am doing something stupid because even when I comment out the setCookie function call nothing happens with any of these.

halfer
  • 19,824
  • 17
  • 99
  • 186
BeNice
  • 2,165
  • 2
  • 23
  • 38
  • 1
    `location` is not an element on the page. `window.location = '2.php'` – Taplar Jun 20 '19 at 17:58
  • window.location = "2.php", see here https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage – varun Jun 20 '19 at 18:01
  • @Taplar not sure what you meant but tried `window.location = "2.php";` gives the same respose. @varun yep saw that answer and that was where I got most of my efforts from. When I click on the button the page definitely refreshes but instead of going to `2.php` it comes straigh back to 1.php. With a normal ` ` it goes to `2.php`just fine! Thanks for the input. – BeNice Jun 20 '19 at 18:40
  • My point is jQuery is meant to be used to manipulate elements on the page. `location` is not an element on the page. It's a property of the window. So it doesn't make sense to use jQuery to manipulate it. – Taplar Jun 20 '19 at 18:45
  • @Taplar but it works. ;) – Kevin B Jun 20 '19 at 20:51
  • @Taplar jQuery is a JS library for doing all sorts of things IMHO. I only tried using it AFTER I could not get native JS to work. I guess I am doing something fundamentally wrong elsewhere on the page (next page???). I will work out a fudge to get around this small problem. If anyone feels like being kind pls upvote to 0. I hate this "lets downvote and not say why" mentality. I thought I had explained and given sufficient info. Ah well. Thanks for your efforts. – BeNice Jun 21 '19 at 09:56
  • You are supposed to provide a [mcve] so we can see what goes wrong (which you also should know by now). As you didn't, and what you did provide works, is why you get downvoted. – Asons Jul 07 '19 at 10:13

1 Answers1

-1

if your button is inside a form, it's better to use preventDefault to avoid those problems. Modify your function with the following:

<script>
    $("#showInfo").on("click", function(e)
    {
        e.preventDefault();

        setCookie("button1","",-1 );
        window.location="2.php";
    })
</script>
GiampaoloGabba
  • 1,428
  • 1
  • 15
  • 21
  • i dont know why i get downvoted... i just tested this in my browser and without the .preventDefault() the behaviour was exaclty as described in the question. Added the e.preventDefault() everything started working as expected... – GiampaoloGabba Jun 20 '19 at 20:35
  • 1
    Sorry that does not work either. I too do not understand why on earth you have been down voted (mind you I have been down voted after about 5 hours of work). There is an arrogant element here who quite happily down vote things without even leaving a comment to say why. It is one of the nastiest things here on SO IMHO. I have upvoted you to help a little. Thanks for your idea. I am giving up on this and will come at the problem another way. – BeNice Jun 21 '19 at 09:53
  • Given that the OP haven't been able to provide a _verifiable sample_ (the posted code work as is), answer such question is not useful to other users, hence likely the main reason you got downvoted. And do note, OP haven't showed they use a `form`, and if they did, this answer might have been useful. – Asons Jul 07 '19 at 10:25
  • @BeNice And note, there is nothing arrogant in downvoting w/o a comment. And if users posting will follow SO guide lines, then rarely, if ever, they get downvoted. I, for example, have created over 2700 posts and can almost count my downvotes on one hand. – Asons Jul 07 '19 at 10:35
  • I thought I **had** explained the problem fully. It was a coding syntax/semantics problem within a function. If it was NOT sufficient a one line request for info would have been helpful. I am here asking for help. I understand that the SO community is mostly full-on professionals but there a lot of us here who do NOT know as much as you nor spend as much time here. The much derided newbie tag could help. I did not ask you to come here and did not ask you to comment. If it said Newb/Amateur **you** would be able to ignore it and those who wanted to help (like me) could help out however poor the – BeNice Jul 07 '19 at 14:53