How to get the current URL protocol, domain and port in JavaScript

1 Answer

0 votes
const protocol = window.location.protocol;
const domain = window.location.hostname;
const port = window.location.port;


const url = `${protocol}//${domain}:${port? port : ""}`

console.log(url); 

  
    
    
/*
run:

"https://fiddle.jshell.net:"
    
*/

 



answered Feb 18, 2021 by avibootz

Related questions

1 answer 163 views
1 answer 199 views
1 answer 283 views
1 answer 228 views
...