简体   繁体   English

将脚本变量作为参数传递

[英]Passing script variables as parameters

I've a javascript variable which i need to pass as a parameter to the included file.我有一个 javascript 变量,我需要将其作为参数传递给包含的文件。 Ex: var var1 = "test";例如:var var1 = “测试”;

Now i need to pass this 'var1' to an include file like this现在我需要将这个'var1'传递给这样的包含文件

<%@include file="text.jsp?param1=" + var1 %> <%@include file="text.jsp?param1=" + var1 %>

Will this work?这会奏效吗? Help me out.帮帮我。

Or, is there any other way around without submitting the form, I need to pass this variable data to that included file which is presented in the same jsp.或者,在不提交表单的情况下是否有其他方法,我需要将此变量数据传递给包含在同一 jsp 中的包含文件。

No, this can't work, because the include will be parsed on the server side, long before the javascript var is available.不,这行不通,因为早在 javascript var 可用之前,将在服务器端解析包含。

Instead, you need to add a request parameter to the page url:相反,您需要在页面 url 中添加请求参数:

<%@include file="text.jsp?param1=${request('someparam')}" %>

No, it won't work.不,这行不通。 Your JSP is compiled and run server-side, and the Javascript is executed much later on the client-side.您的 JSP 已编译并在服务器端运行,而 Javascript 则在客户端稍后执行。

JS processed after JSP. JS 在 JSP 之后处理。 Learn how server side and client side code is executed了解如何执行服务器端和客户端代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM