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.

Richard
  • 8,193
  • 28
  • 107
  • 228
  • 8
    if you want efficiency, use a serverside redirect. – Jonas Wilms Jul 10 '17 at 16:37
  • _"Does this mean that the page will get redirected before the body tries to load?"_ Yes – j08691 Jul 10 '17 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. – Richard Jul 10 '17 at 16:38
  • Possible duplicate of [Where should I put – Josh Beam Jul 10 '17 at 16:38
  • Parser will block body rendering when it encounters a script tag in `head` element. – Josh Beam Jul 10 '17 at 16:39
  • Seems there are results if you search [_redirect http https aws elb_](https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb/) – mplungjan Jul 10 '17 at 16:40
  • Hi mplungjan, thanks for the feedback. Yes, I have tried about 20 different bits of advise on how to do server rewriting. Modified the conf files, etc. But cannot get it to work. I have posted the question on this, but no answers work. https://serverfault.com/questions/857962/apache-rewrite-rule-not-working and https://serverfault.com/questions/857915/apache-where-to-add-rewrite-rule – Richard Jul 10 '17 at 16:44

3 Answers3

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.)

Ben
  • 2,200
  • 20
  • 30
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.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
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

JuanGG
  • 834
  • 7
  • 11