Prabhu
Prabhu

Reputation: 13335

Removing querystrings from the URL before page refresh

After a new user signs up, I need to redirect the user to the home page and display a twitter-style welcome message. I initially tried using the jquery cookie plugin to store my message and display it on the redirected page if the cookie is present, but the problem was that it didn't work across all browsers. Firfox on Safari does not delete the cookie, so the message keeps showing everytime the browser is refreshed. Here's the code:

if ($.cookie("message")) {
    TwentyQuestions.Common.ShowMessageBar($.cookie("message"), 7000);
    $.cookie('message', "any_value", { expires: -10 })
}

So I decided to use querystring instead, but now the problem is similar. When the home page load, the query string is detected and the message is displayed. But how do I remove the querystring from the URL so that the message doesn't show everytime the page is refreshed?

Thanks!

Upvotes: 3

Views: 6361

Answers (5)

mu is too short
mu is too short

Reputation: 434665

You can do this with cookies but you have to delete the cookie properly. Setting an expiry date in the past works with some browsers but not others as you've found, the proper way to delete a cookie with the jQuery cookie plugin is to send in a null; from the fine manual:

@example $.cookie('the_cookie', null);
@desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain used when the cookie was set.

So delete it with this:

$.cookie('message', null);

and the cookie approach should work fine.

Upvotes: 0

Mike Samuel
Mike Samuel

Reputation: 120506

location = location.pathname + location.hash

This will of course lose any POST data, but if you've got a query string, they probably arrived at your site via GET anyway.

It should have no effect if the location has no query component.

Upvotes: 0

user342706
user342706

Reputation:

Could you do:

window.location.href = window.location.href.split('?')[0];

It works, but I'm not for sure if this is what you are looking for?

Upvotes: 6

Keltex
Keltex

Reputation: 26426

Maybe the problem is you're trying to do everything on the client side. Instead you should set a persistent cookie associated with the user. Then in the back-end, the first time the homepage is displayed to this user, show you welcome message. Also clear whatever "first time user" flag for this user on the server side. Then the next time the user visits this page they won't see the message.

You can also do a SO like thing where if a user visits your website and the cookie doesn't exist, you can display the "Welcome first time user" message.

Upvotes: 1

marcosfromero
marcosfromero

Reputation: 2853

Instead of using querystring you can use hash.

Redirect to home page with a special hash and when entering, just remove it.

Something like:

if(document.location.hash == '<special hash>') {
    TwentyQuestions.Common.ShowMessageBar(...);
    document.location.hash='';
}

Upvotes: 1

Related Questions