DDan
DDan

Reputation: 8276

Is it considered as bad practice to redirect in header with Javascript?

Let's assume that I load a filtering page, and based of an earlier set cookie, I want to reload the page by adding a few query parameters to my url. Something like this:

<head>
    <script>
        if (need_to_load_with_different_params) {
            window.location.href = window.location.href + params_from_cookie;
        }
    </script>
</head>

I don't remember seeing web applications using this kind of pattern. Is there a good reason for that? Should I move such logic to server-side by all means?

(using jquery.cookie to simplify cookie reading if that makes any difference)

Upvotes: 3

Views: 1303

Answers (1)

oliverpool
oliverpool

Reputation: 1671

As commented by @Rory McCrossan, it would be better to do this server side.

Several reasons for that:

  • The user could have JavaScript disabled
  • The navigation will feel much cluttered (client request -> server response -> JavaScript parsing -> client request -> server reponse -> HTML parsing before actually having the whole page)

Original Answer: It shouldn't be considered secure: in any case the rest of the content of the page will be downloaded by the user (if JavaScript is disabled, the redirect won't happen)

Upvotes: 3

Related Questions