Using JavaScript within a JSP tag

后端 未结 4 779
猫巷女王i
猫巷女王i 2021-01-05 04:00

I\'ve seen this question regading the importing of js-files related to the tag content itself. I have a similar problem, here I have a jsp tag that generates some HTML and h

4条回答
  •  轮回少年
    2021-01-05 04:15

    You should strive for javascript in its own files. This is usually done with Progressive Enhancement. But some times you don't have a choice, for instance when the same JSP renders pages in different languages. Here's a real-life example:

    The JSP:

        
        
    

    The javascript (article_admin.js):

     /*global NP_ArticleAdmin, jQuery, confirm */  
     NP_ArticleAdmin = function ($) {  
         var text;  
    
         function delete_article(event) {  
             var article = $(this).parents("li.article"),  
             id = article.attr("id"),  
             name = article.find("h3.name").html();  
             if (confirm(text.please_confirm_deletion_of + name + text.this_cannot_be_undone)) {  
                 $.post("/admin/delete_article", {id: id});  
                 article.fadeOut();  
             }  
             event.preventDefault();  
             return false;  
         }  
    
         function initialize(data) {  
             text = data.text;  
             $("#articles a.delete").click(delete_article);  
         }  
    
         return {initialize: initialize};  
     }(jQuery);
    

    In this example, the only javascript in the JSP-file is the part that needs to be there. The core functionality is separated in its own js-file.

提交回复
热议问题