javascript 脚本运行在服务器还是客户端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10516103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Script run on server or client?
提问by ScarCode
I have a thttpd server set-up which has the following html file. When I give address server-address/file-name.html on a standard web browser errors on the script is logged in error console of browser. I am confused about where the script is run actually? Is it on the client side or are the error messages just passed on to browser by server?
我有一个 thttpd 服务器设置,其中包含以下 html 文件。当我在标准 Web 浏览器上给出地址 server-address/file-name.html 时,脚本上的错误记录在浏览器的错误控制台中。我对脚本实际运行的位置感到困惑?是在客户端还是错误消息只是由服务器传递给浏览器?
My requirement is to run script on a server to generate dynamic web pages upon client interaction.
我的要求是在服务器上运行脚本以在客户端交互时生成动态网页。
<html>
<head>
<title>Entitled Document</title>
<script language="JavaScript" >
Function Java_Scriptfn()
{
alert('Test'
}
</script>
</head>
<body>
<input type="button" value="Script_Check" onclick="Java_Scriptfn()">
</body>
</html>
回答by Quentin
回答by Polygnome
JavaScript that is embedded in a HTML site (either inline or load from another file) is always executed client-side (that means in your browser).
嵌入在 HTML 站点中的 JavaScript(内联或从另一个文件加载)始终在客户端(这意味着在您的浏览器中)执行。
If you want it to be executed, server-side, you need something like node.js.
如果你想让它在服务器端被执行,你需要像 node.js 这样的东西。
回答by Elliot Bonneville
It's client side code; any Javascript files included in an HTML page will run client-side (although they cantalk to a server, that's different).
这是客户端代码;HTML 页面中包含的任何 Javascript 文件都将在客户端运行(尽管它们可以与服务器通信,但这是不同的)。