Javascript Location Protocol with Example

Updated on 22 Mar 2012,
Published on 10 Jul 2008

Javascript location protocol property sets or returns the current protocol used to access the web page. Location protocol returns "http:" if the current web page URL starts with "http:". If the current URL starts with secure connection "https:" then location.protocol returns "https:". URL accessed through file transfer protocol returns the "ftp:" as a result to the location protocol property. You can build the URLs using Javascript location functions and prefix the protocol that is being used by the web pages using location.protocol property. Sometimes, location.protocol property helps in generating relative paths and links having the same protocol access method for each item placed in the web page such as images, flash movies and HTML anchor <a> tag links. You can use the location protocol property to apply the current protocol of the web page for each object on the web page. This also resolved the issue of alert box for secure sites while displaying the secure and unsecure content on the web page. Coz this message box pop ups only when there are links starting with "http:" on the secure web page accessed using "https:"

JavaScript Window Location Object Examples:

You can see the live samples and examples of JavaScript Window Location Object from the following links:

Syntax for Javascript Location Protocol Property

location.protocol

or

window.location.protocol

You can use any of the above syntax to retrieve the location protocol name.

Following syntax shows how to reset the location protocol property value:

location.protocol = "value";

e.g.: location.protocol = "https:";

Example of Javascript Location Protocol Property

<script type="text/javascript" language="javascript">
    function locationProtocol() {
        alert(window.location.protocol);
    }
</script>

Output:

You can see the output of above discussed code from the following link:

JavaScript window.location.protocol

Continue to next tutorial: Javascript Location Pathname with Example to learn how to get the path to the web page accessed by the web browser.

0 Responses to "Javascript Location Protocol with Example"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email