2

I'm new to javascript might be this question looks silly. How to check whether the html files are loading from local file system or server using javascript?

For example when I open the html files from my local system (/home/user/1.html) browser shows in url

file:///home/user/1.html

But if i load 1.html file into my local server then, if i access that file browser shows in url like below

http://localhost/GUI/1.html

I want to find whether files are loading from my server or local file system using java script.

Is there any way to find this using java script method.

3
  • 2
    location.protocol Commented Jul 9, 2018 at 7:38
  • @CertainPerformance - Can you provide some more details? Commented Jul 9, 2018 at 7:40
  • console.log(location.protocol) Commented Jul 9, 2018 at 7:41

1 Answer 1

4

You can use the window.location object to do that.

use window.location.protocol property.

The following line of code will return true if the file is being served from the file system.

window.location.protocol == "file:"; 

It basically checks if the protocol being used is file or not.

Sign up to request clarification or add additional context in comments.

2 Comments

This only check for the Html file loaded in the browser.. all consecutive Javascript file requests generated from html page will not get checked.
because the question asked is about HTML files only.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.