JavaScript can tell you whether it’s running on a server or locally.

この記事は約2分で読めます。
スポンサーリンク

Things I want to do

This code uses JavaScript to check whether the program is running on a server or locally.

(This is used when running test code locally and production code on the server.)

スポンサーリンク

Protocol-based verification

You can check this in the protocol part of the URL.

When running locally, the URL protocol is ‘file:’, and when running on a server, it is ‘http:’ or ‘https:’.

Therefore, you can check it with the following code.

if (location.protocol == "file:") {
    /////local
} else {
    //////server
}
スポンサーリンク

Result

I was able to check whether the program was running on the server or locally using JavaScript and then process it differently.

コメント

タイトルとURLをコピーしました