
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to return the protocol (http or https) of the web page with JavaScript?
In this tutorial, we will look at how to find which protocol is being used by a web page. A web page mostly uses http or https protocol. A protocol is a type of standard which is used to specify, how the data is transferred or transmitted between different sets of computer.
HTTP − HTTP is a protocol for retrieving resources such as HTML pages. It is one of the most essential and the backbone for all types of data exchange that happens over the internet. HTTP is a client server protocol which means that all the requests are done by the client which are mostly Web browsers. A full document is constructed by many sub documents such as text, photos, videos, scripts and others.
HTTPS − HTTPS or Hyper Text Transfer Protocol Secure is an encrypted variant of the HTTP protocol. All communication between a client and a server is encrypted using SSL or TLS. This secure connection enables clients to communicate sensitive data with a server in a secure manner, such as while banking or buying online.
We shall use windows.location.protocol or url.protocol to get the protocol that is being used by the page or the URL that has been specified. It will return protocols such as −
HTTP
HTTPS
FTP
MAILTO
FILE
Syntax and Parameters
To return the protocol of the current page
windows.location.protocol
To return the protocol of some other page or URL
url = new URL("https://www.url.com/") proto = url.protocol
Return Value − It returns an object which is of the data type String. The string contains the value of the Protocol that is currently being used by the webpage or the url that has been passed to the function.
For example, https://www.tutorialspoint.com/ will return https −
whereas, http://www.columbia.edu/~fdc/sample.html will return http −
Example 1
<!DOCTYPE html> <html> <body> <script> document.write("https or https: <br>The protocol is: "+window.location.protocol); </script> </body> </html>
In the above code, we find the protocol that the current page is using, we use windows.location to find the url of the current page and then url.protocol method to get the protocol of the current page which is https −
Windows.location − The window.location object is used to retrieve the current page address that is the URL and can also be used to redirect the browser to a different page.
Let us take a look at this with the help of an example as follows.
Example 2
<!DOCTYPE html> <html> <body> <script> url_object = new URL("http://www.columbia.edu/~fdc/sample.html"); document.write("https or https: <br>The protocol is: "+ url_object.protocol); </script> </body> </html>
In the above code, we find the protocol that the url page is using, we use new URL to create a new url for which we then use the url.protocol method to get the protocol of the current page which is http −
Example 3
<!DOCTYPE html> <html> <body> <script> url_object = new URL("mailto:xyz@gmail.com"); document.write("https or https: <br>The protocol is: "+ url_object.protocol); </script> </body> </html>
In the above code, we find the protocol that the url page is using, we use new URL to create a new url for which we then use the url.protocol method to get the protocol of the current page which is mailto −
Conclusion
In this tutorial, we saw how to find the protocol which a web page is using. The two protocols that we discussed were: HTTP and HTTPS. We learnt about these two protocols in detail, including how they work and where they are mostly used. Apart from these topics, we also saw how to use the windows.location.protocol or url.protocol to find out the protocol being used in a web page.
- Related Articles
- How to get the protocol and page path of the current web page in JavaScript?
- Difference Between HTTP and HTTPS
- How do I redirect my web page with JavaScript?
- Differentiate between HTTP and HTTPS in Computer Network.
- Creating ‘Copy to Clipboard’ feature on a web page with JavaScript
- How to return true if the bottom of the page is visible using JavaScript?
- How to completely fill web page with HTML canvas?
- How to make the web page height to fit screen height with HTML?
- Set the navigation bar to stay at the top of the web page with CSS
- Display all the values of an array in p tag on a web page with JavaScript
- JavaScript: How to return True if the focus is on the browser tab page?
- How to return an object by parsing http cookie header in JavaScript?
- As HTTP is a stateless then how to maintain the session between web browser and web server?
- How to return the color of the text with JavaScript?
- Get the size of the screen, current web page and browser window in JavaScript
