将 GET 参数传递给 JavaScript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9806266/
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
Pass a GET parameter to a JavaScript function?
提问by Aishwarya Shiva
I have this code
我有这个代码
<%=out.write("<input
type=\"hidden\"
id=\"tid\"
value=\""+request.getParameter("id").toString()+"
\"/>")%>
<script type="text/javascript">
getPage(document.getElementById("tid").value)
</script>
This code creates a hidden field with the value came from
此代码创建一个隐藏字段,其值来自
<site root>/viewPage.jsp?id=erwdf
<site root>/viewPage.jsp?id=erwdf
url and pass the value of this hidden field to a jsp function. When I ran this code on Tomcat it gave an error as
url 并将此隐藏字段的值传递给 jsp 函数。当我在 Tomcat 上运行这段代码时,它给出了一个错误
The method print(boolean) in the type JspWriter is not applicable for the arguments (void)
JspWriter 类型中的方法 print(boolean) 不适用于参数 (void)
on JSP code line I given above. So am I doing anything wrong or is there any alternative method to pass a GET parameter to a JavaScript function? I don't know much about Javascript just started to learn it.
在我上面给出的 JSP 代码行上。那么我做错了什么还是有任何替代方法可以将 GET 参数传递给 JavaScript 函数?我对 Javascript 了解不多,刚开始学习。
回答by BalusC
Why so overcomplicated with a hidden field?
为什么隐藏字段如此复杂?
Just do
做就是了
getPage('<%=request.getParameter("id")%>');
Or easier, with EL
或者更容易,使用 EL
getPage('${param.id}');
You may only want to escape special JS characters by Apache Commons LangStringEscapeUtils
, otherwise the generated JS code may break whenever the parameter value contains a single quote or any other special JS character.
您可能只想通过Apache Commons Lang转义特殊的 JS 字符StringEscapeUtils
,否则当参数值包含单引号或任何其他特殊的 JS 字符时,生成的 JS 代码可能会中断。
getPage('<%=StringEscapeUtils.escapeJavaScript(request.getParameter("id"))%>');
Or when in EL
或者在 EL
getPage('${util:escapeJS(param.id)}');
See also:
也可以看看:
回答by dldnh
I believe you meant <%out.write
instead of <%=out.write
我相信你的意思是<%out.write
而不是<%=out.write
about the other issue from the comments, this will assist with getPage and perform escaping of quotes, other special chars...
关于评论中的另一个问题,这将有助于 getPage 并执行引号、其他特殊字符的转义...
<script type="text/javascript">
getPage("<% try {
out.write(URLEncoder.encode(request.getParameter("id").toString(), "UTF-8"));
} catch (Exception e) {
} %>")
</script>
回答by Reinard
You don't have to store it in a hidden field to access it from js. You can read it from the documents location. I personally use a method like this to grab GET parameters from my url.
您不必将其存储在隐藏字段中即可从 js 访问它。您可以从文档位置阅读它。我个人使用这样的方法从我的 url 中获取 GET 参数。
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
var id = getUrlVars()['id'];
var id = getUrlVars()['id'];