mk-toolブログ

エンジニアと家のことをごちゃごちゃと書いてます

javascriptでプロトコル、ポート、ホスト名を取得

プロトコル+ポート+ホスト名をなんと呼ぶのかわかりませんが、 https://xxx.com:3000 みたいなものを出力する。

80番ポートの時は、ポート番号はつけないようにしている。

const protocol = window.location.protocol
const port = window.location.port
const host_name = window.location.hostname
const protocol_and_fqdn = `${protocol}//${host_name}${port!=80?':'+port:''}`
console.log(protocol_and_fqdn)