0

I have some javascript

<script>
    // some java code that doesn't matter right now

    localStorage.setItem("myName", "Bob");
    alert(localStorage.myName);
<script>

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;

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
wjhplano
  • 611
  • 2
  • 13
  • 31

1 Answers1

4

Because look at the source code of the page that this line generates

var hi5 = <%= "getMyName();" %>

It would render something this

var hi5 = BOB

Do you have a variable BOB? No. You are missing the quotes which would make it a string.

var hi5 = "<%= getMyName(); %>";
          ^                   ^^
epascarello
  • 204,599
  • 20
  • 195
  • 236