1

My LogIn action originally looked like this:

return new RedirectResult(nameValueCollection["ReturnUrl"]);

But, I would like to know whether the client has JavaScript enabled before the home page loads, so I changed it to this:

return View(new LogInViewModel { ReturnUrl = nameValueCollection["ReturnUrl"] });

And send the following view instead of the instant-redirect:

@model Obr.Web.Models.LogInViewModel

@{
    Layout = null;
    string noJSReturnUrl = Model.ReturnUrl + (Model.ReturnUrl.Contains("?") ? "&" : "?") + "noJS=true";
}

<!doctype html>

<html>
    <head>
        <title>Loggin in...</title>
        <meta http-equiv="REFRESH" content="1;url=@noJSReturnUrl">
        <script type="text/javascript">
            window.location = "@Model.ReturnUrl";
        </script>
    </head>
    <body>
        <noscript>
            Loggin in...<br />
            <a href="@noJSReturnUrl">Click here if you are not redirected promptly.</a>
        </noscript>
    </body>
</html>

The idea is that if the user does not have JavaScript enabled, they see a brief loading message, and the home page loads after a second. If JavaScript is enabled, the page reloads instantly. In the future I could even post to the server the dimensions of the viewport and such.

Does this look like it would work? If the window.location command takes longer than a second to run, will it be interrupted by the meta refresh, or does it block that refresh? I am hoping the latter, so I don't need to increase the delay for those non-js people.

I figure my new way adds a little extra weight to the payload of the redirect, but it's not an extra round-trip or anything, is it? The redirect happens anyway, does it not?

Update: I neglected to mention a very important point. I do not actually have control over the login screen itself, only the page it posts to. This code is part of a product that relies on an external authentication mechanism.

2 Answers 2

1

You do not need the extra redirect just to detect javascript. On the original form where the user logs in, create a hidden form element javaScriptEnabled with a default value of false. Then use JavaScript to set the value to true. Then you can read this value in the handler. If it's true, then JS is enabled.

No extra page needed.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah that is perfectly valid but I neglected to mention that is not an option for me. I ship a product that gets logged into externally. Your solution would be perfect, though.
0

Since you can't change the original login form then your solution looks good. It won't display anything to the user who has JS and should look just like another redirect, with just an extra hop.

Once you write a new url to window.location then the browser will stop processing the current page's js and timers and everything and simply move on to retrieving/processing the next page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.