Search code examples
javascriptscriptlet

How to access java scriplet varible from external java script file?


I have a variable defined in my.jsp file.

        <%String myvarible = "testing";%>

my.js is imported into my.jsp (just before the end of body). I try to access the myvarible in my.js. I tries several methods on the internet. But failed.

Any help is welcome


Solution

  • Generally, I put the assign variable on some data-attribute of some html tag. Then I can access it on my js file.

    my.jsp

    <div class="foo" data-jsp-variable="<%= myvariable %>"></div>
    

    my.js

    var bar = $('.foo').data('jsp-variable')