最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

running javascript function inside jsp - Stack Overflow

programmeradmin0浏览0评论

Is it possible to run javascript functions inside jsp tags? I'd like to run a sudden function as many times as there's objects in my ArrayList. Below doesen't work, but I hope it gives an idea of what I'm trying to achieve.

    <script>
    function test(){
        alert();
    }
    </scripts>


<% 
ArrayList<Marker> list = new ArrayList<Marker>();

list = (ArrayList<Marker>)request.getAttribute("markers"); 

for(int i = 0; i < list.size(); i++){
    %>
        <script>
        <%
        test();
        %>
        </script>
    <%
}
%>

Is it possible to do it with something like ?

<c:forEach var="name" items="${markers}">
   <%-- call my javascript function --%>

</c:forEach>

Is it possible to run javascript functions inside jsp tags? I'd like to run a sudden function as many times as there's objects in my ArrayList. Below doesen't work, but I hope it gives an idea of what I'm trying to achieve.

    <script>
    function test(){
        alert();
    }
    </scripts>


<% 
ArrayList<Marker> list = new ArrayList<Marker>();

list = (ArrayList<Marker>)request.getAttribute("markers"); 

for(int i = 0; i < list.size(); i++){
    %>
        <script>
        <%
        test();
        %>
        </script>
    <%
}
%>

Is it possible to do it with something like ?

<c:forEach var="name" items="${markers}">
   <%-- call my javascript function --%>

</c:forEach>
Share Improve this question edited Sep 26, 2015 at 15:39 JonCode asked Sep 26, 2015 at 14:50 JonCodeJonCode 2411 gold badge8 silver badges20 bronze badges 3
  • you probably mean test('<%= list.get(i).name %>') – dimakura Commented Sep 26, 2015 at 14:51
  • yes it can be done but for that you have to include the script tag – brk Commented Sep 26, 2015 at 14:52
  • I tried using script tags, but no luck. Can you tell what I'm doing wrong? – JonCode Commented Sep 26, 2015 at 15:10
Add a comment  | 

3 Answers 3

Reset to default 8

Below correction in your code will work fine for you

<script>
    function test(){
        alert("Hello"); // added sample text
    }
 </script>


<% 
ArrayList<Marker> list = new ArrayList<Marker>();

list = (ArrayList<Marker>)request.getAttribute("markers"); 

for(int i = 0; i < list.size(); i++){
    %>
        <script>
        test(); //No need to put java script code inside scriptlet
        </script>
    <%
}
%>
<% 
ArrayList<Marker> list = new ArrayList<Marker>();

list = (ArrayList<Marker>)request.getAttribute("house"); 

for(int i = 0; i < list.size(); i++){
    %>
      <script>
         test('<%= list.get(i).name %>');
      <script>
    <%
}
%>
<script>
    function test(i){
        alert(i);
    }
</script>

By writing your js code within the script tag inside a out.println() has shown below:

<% 
    ArrayList<Marker> list = new ArrayList<Marker>();

    list = (ArrayList<Marker>)request.getAttribute("markers"); 

       for(int i = 0; i < list.size(); i++){

              out.println("<script>test();</script>");
  }
%>
发布评论

评论列表(0)

  1. 暂无评论