jsp中通过java访问javascript
我的代码目前看起来像这样
<%
if (request != null) {
bustOut;
}
%>
<script language="javascript">
function bustOut(){
var newWin = window.open("the real url", "subWindow","height=500,width=700,resizable=yes,scrollbars=yes");
}
</script>
How do I call the javascript function inside the Java code?或者说这是不可能的?
My code currently looks like this
<%
if (request != null) {
bustOut;
}
%>
<script language="javascript">
function bustOut(){
var newWin = window.open("the real url", "subWindow","height=500,width=700,resizable=yes,scrollbars=yes");
}
</script>
How do I call the javascript function inside the Java code? Or is that not possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JSP 在网络服务器上运行,并根据网络浏览器请求生成/生成 HTML/CSS/JS 代码。网络服务器将 HTML/CSS/JS 发送到网络浏览器。 Web 浏览器运行 HTML/CSS/JS。因此,您只需让 JSP 将其逐字打印为 JS 代码即可。
或者,更好,使用 EL
(请注意我将属性名称更改为
foo
因为request
代表HttpServletRequest
这可能会让其他人感到困惑,因为它永远不会null
)无论哪种方式,当条件为真时,生成的 HTML(您应该通过在浏览器中打开页面,右键单击它并选择查看源代码看到)应该如下所示:
现在是否打开你头顶上有一个灯泡吗?
JSP runs at webserver and generates/produces HTML/CSS/JS code upon a webbrowser request. Webserver sends HTML/CSS/JS to webbrowser. Webbrowser runs HTML/CSS/JS. So, you just have to let JSP print it literally as JS code.
or, better, with EL
(note that I changed the attribute name to
foo
becauserequest
stands forHttpServletRequest
which might confuse others since this is nevernull
)Either way, the generated HTML (which you should see by opening page in browser, rightclicking it and choosing View Source) should look like this when the condition was true:
Does it now turn on a light bulb above your head?
你不能从java调用javascript函数
你的java代码在服务器上执行,而javascript在客户端上执行。
您似乎需要的是有条件地在文档加载时打开一个新窗口。为此:(
上面是用于检测文档加载的 jQuery。您可以将其替换为纯 javascript -
window.onload = function() {..}
或document.onload = function() { ..}
我认为)请注意
request != null
是毫无意义的条件 - 在 JSP 中请求永远不会null
。最后 - 使用 jstl 标签(就像我展示的那样)而不是 java 代码(scriptlet)。
You can't call javascript functions from java
Your java code is executed on the server, and the javascript - on the client.
What you seem to need is conditionally open a new window on document load. For that:
(that's jQuery above for detecting document load. You can replace it with pure javascript -
window.onload = function() {..}
ordocument.onload = function() {..}
I think)Note that
request != null
is meaningless condition - the request is nevernull
in a JSP.And finally - use jstl tags (like I showed) and not java code (scriptlets).