How to check if js code is running on node server or on the client

前端 未结 1 1648
臣服心动
臣服心动 2020-12-18 19:23

I want to determine whether my js code is running on the node server or on the client, and store it into a variable isServer = true if it is running on the serv

相关标签:
1条回答
  • 2020-12-18 20:03

    You could use this:

    function is_server() {
       return ! (typeof window != 'undefined' && window.document);
    }
    

    As the global window.document object is only present in the browser context.

    0 讨论(0)
提交回复
热议问题