Javascript to get the server time, javascript Server
JS is a script running on the client, rather than running on the server. In general, the time obtained through JS is the time on the guest's local computer, in order to make the time on the webpage not different from the visitor's computer, we need to directly obtain the server time in JS.
The following is the complete javascript code for getting server time. It is a complete example of getting server time using javascript, and js dynamically refreshes server time.
<! DOCTYPE html>
Train of Thought Analysis:
The concept of server time is vague. Because the server end may not be composed of one machine after all. The simplest case is the web server and db server. Which one are you talking about?
For web servers.
Then, the new Date () in java or. net is OK.
Do not add new Date in <script> on the webpage. That is only the client time.
For db servers.
You need to connect to the database and obtain it through SQL. For example, oracle is select sysdate from dual.
Example:
There are many methods for js to obtain such server-side time. The general idea is similar.
1.
<Script type = "text/javascript">
Var date = <% = date (the time obtained on the server side and the Content Retrieval Method has been analyzed above) %>
</Script>
2. <script type = "text/javascript" src = ".../ScriptServlet"> </script>
Write a js file into the Servlet response stream so that var nowDate = server-side time
3. ajax. I don't want to talk about it anymore. There are a lot of resources on the Internet.
The above is all the content of this article. I hope you will like it.