3

I have the following web page that redirects http traffic to https.

<head>
<script type="text/javascript">
    var loc = window.location.href+'';
    if (loc.indexOf('http://www.') == 0 && loc.indexOf('.com') > -1) {
        window.location.href = loc.replace('http://www.','https://www.');
    } else if (loc.indexOf('http://') == 0 && loc.indexOf('.com') > -1  && loc.indexOf('www.') == -1) {
        window.location.href = loc.replace('http://','https://www.');   
    }
</script>
</head>
<body>
   ...
</body>

Question

I have placed the redirect javascript above the html body. Does this mean that the page will get redirected before the body tries to load?

I am trying to make this code as efficient as possible.

Any advise appreciated.

7
  • 8
    if you want efficiency, use a serverside redirect. Commented Jul 10, 2017 at 16:37
  • "Does this mean that the page will get redirected before the body tries to load?" Yes Commented Jul 10, 2017 at 16:38
  • Thanks Jonas, I am hosting my page on AWS ELB. I have tried for about a week to get server side redirect working, but cannot seem to get it, so this is my last resort. Commented Jul 10, 2017 at 16:38
  • Possible duplicate of Where should I put <script> tags in HTML markup? Commented Jul 10, 2017 at 16:38
  • Parser will block body rendering when it encounters a script tag in head element. Commented Jul 10, 2017 at 16:39

3 Answers 3

3

To answer your question, yes, the script will be executed before everything below it. However, if Javascript is disabled, or unsupported, you will have issues.

Typically, I would recommend you use a server-side redirect. If you know PHP, that is one simple option. If you don't, and this is on an Apache hosted server (either localhost or external) I suppose you could use a .htaccess file It may also be possible that your webhost's CPANEL allows you to do this without editing anything. Some CPANELs for example have an option for auto-redirecting of HTTP links HTTPS. If your CPANEL doesn't, you could (I suppose) redirect every HTTP page to HTTPS using whatever options your CPANEL gives you.

If you absolutely must have the most efficient option, it's .htaccess (although that is also the most painful, so I would recommend CPANEL or PHP if you are afforded those options.)

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

Comments

1

HTML pages are processed from left to right, top to bottom unless there is specific instructions to the contrary.

Given that your JavaScript is not encapsulated in a function or assigned as a callback, it will run prior to the HTML that follows it.

Comments

1

By setting window.location.href you are telling the browser to leave, so whatever you have after that wont be executed. Obviously, if you had something before that (css, other scripts, ...), it would have already been processed.

The thing is that the browser might have started downloading / parsing other resources before starting to execute any scripts (It depends on the engine.)

The best way is to redirect server-side to make it faster, unless the logic had to be in your web application.

More info

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.