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:”

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>

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.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.