location.host vs location.hostname and cross-browser compatibility?

后端 未结 6 1455
情话喂你
情话喂你 2020-11-29 14:59

Which one of these is the most effective vs checking if the user agent is accessing via the correct domain.

We would like to show a small js based \'top bar\' style

6条回答
  •  旧巷少年郎
    2020-11-29 15:35

    If you are insisting to use the window.location.origin You can put this in top of your code before reading the origin

    if (!window.location.origin) {
      window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
    }
    

    Solution

    PS: For the record, it was actually the original question. It was already edited :)

提交回复
热议问题