内联Java导致javascript无法执行 [英] inline java makes causing javascript to not execute
问题描述
我有一些javascript
I have some javascript
<script>
// some java code that doesn't matter right now
localStorage.setItem("myName", "Bob");
alert(localStorage.myName);
<script>
它工作得很好(给出警告消息说鲍勃).很好,但我真正想要的是将java变量传递给javascript变量,然后将其打印出来. 但是当我将这些行放入其中时...
it works just fine (giving an alert message that says Bob). that's fine and dandy but what I really want is to pass a java variable to a javascript variable and have that print out instead. But when I put these lines into it...
var hi5 = <%= "getMyName();" %>
localStorage.someName = hi5;
退出.在此之前的任何JavaScript都可以正常工作.但之后的所有JavaScript都不会显示.
It quits. Any javascript before that works fine. but any javascript after it just doesn't show up.
现在<% %>
标记的语法可能不正确,但实际上并没有给我带来任何错误
now the <% %>
tags might not be in the exact syntax but it doesn't really give me any errors
我确定我会忽略某些东西,但不确定会是什么.我该怎么办?
I'm sure I'm overlooking something but I'm not sure what it would be. What can I do?
推荐答案
因为查看此行生成的页面的源代码
Because look at the source code of the page that this line generates
var hi5 = <%= "getMyName();" %>
它会渲染出这样的东西
var hi5 = BOB
您有一个可变的BOB吗?否.您缺少引号,而该引号会使它成为一个字符串.
Do you have a variable BOB? No. You are missing the quotes which would make it a string.
var hi5 = "<%= getMyName(); %>";
^ ^^
这篇关于内联Java导致javascript无法执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!