Callat
Callat

Reputation: 3044

Force a route cleanly with javascript

I'm working with a legacy app's UI and the path that links to this app is a default:

something/fldr

Whenever that page loads it forces a fldr/landing.asp page. We want to get it to go to other.asp instead of landing.

My approach for this is to use:

if (document.readyState === "interactive") {
               if(location.href == 'https://www.something.com/fldr'){
            location.href="https://www.something.com/other.asp";
              }
            }

Doing this causes a page stutter, where the landing.asp loads, shows for like 2 seconds and then refreshes to the correct page.

Is there a standard method for doing something like this in JS or jQuery? I feel like there is a way to make the page hang up until the if statements executes rather than try to load the wrong page. But I can't for the life of me remember what it is. I've handled this on the back end by forcing the correct page to return in the API but I still feel like this is something that can be resolved with only JS.

Note: The route names are made up since this is a stripped down problem of a legacy app.

Upvotes: 0

Views: 39

Answers (1)

Dmitriy Khaykin
Dmitriy Khaykin

Reputation: 5258

JavaScript (when running in a browser) is a client-side technology.

That means it cannot run without the page partially loading after the page has been served and sent to the user's browser (client). The browser begins loading resources and parsing scripts and code, and your script will execute in the order it is parsed. This is, in fact, the delay you're experiencing.

While you may possibly tweak this to make the location.href change execute in some earlier part of this process, there is no way to avoid a partial page load prior to the client-side redirect you have implemented.

Essentially, there is a better way to do this, one which will reduce the redirect delay to be imperceptible to a user.

Making this change at the web-server level is the ideal solution; however, first consider, is that even needed?

First, before implementing a redirect, I would suggest to look in the IIS settings and see if there is a default document set to fldr/landing.asp;

You can then just change that setting to make the default document to what you need.

Here's an example for IIS how to do this.

If there is not a default document or if there is some other code or application logic that is forcing landing.asp to load, then you would set up a 301 Permanent Redirect for that URL on the web server.

Here are IIS docs on setting this up.

IF for some reason the above options are unavailable to you (don't have access to web server, etc.), then the best you can do is ensure that script is the first thing in the page before any other scripts, stylesheets, etc., are loaded.

Another hacky thing that might work is just replacing the entire content of landing.asp with other.asp and call it a day :)

That is a last resort of course, and hopefully you can just change the default document and that will handle it.

Upvotes: 1

Related Questions