gezgin
gezgin

Reputation: 207

How can i convert java String to javascript string?

i used these code but nothing seen on document

            <%String movie_name ="Matrix"; %>
            <script type="text/javascript">
                var movie_name="";
                movie_name= <%= movie_name%>;
                document.write(movie_name);
            </script>

so anyone can help me to convert java string to javascript string ?

Upvotes: 2

Views: 9885

Answers (2)

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28379

you need to wrap your output in quotes (I'm assuming this is JSP?)

movie_name = "<%= movie_name %>";

See, when this is written out the browser tries to interpret it, so without quotes you wind up with something that looks like...

movie_name = Men In Black;

Since this is obviously a massive syntax fail, the browser just quits trying and silently fails (though you should see a log of what it didn't like).

When you wrap the output in quotes then everything falls into place HOWEVER, make sure to convert any " in your java string to \" when you print it out, or you'll have more of the same trouble.

However, as the other answers suggest, you're re-inventing the wheel here and should just do this the prescribed way, as per Nishant's advice.

Upvotes: 2

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340903

This might do it (missing quotes):

movie_name="<%= movie_name%>"

Also looking at your sample code you can replace it completely with:

<%= movie_name%>

Finally consider using .

Upvotes: 6

Related Questions