Javascript Browser Location origin Property

Introduction

Return the protocol, hostname and port number of a URL.

var x = location.origin;

View in separate window

 Code:
 <!DOCTYPE html>
 <html>
 <body>
                               /*  w w  w .ja va2 s.co m*/
 <button onclick="myFunction()">Test</button>
                               
 <p id="demo"></p>
                               
 <script>
 function myFunction() {
   var x = location.origin;
   document.getElementById("demo").innerHTML = x;
 }
 </script>
                               
 </body>
 </html>

The origin property returns the protocol, hostname and port number of a URL.

If the port number is not specified in the URL (or if it is the default port - like 80, or 443), some browsers will not display the port number.

This property is read-only.

The origin property returns a String representing the protocol.




PreviousNext

Related