在JSP标记中使用JavaScript [英] Using JavaScript within a JSP tag

查看:93
本文介绍了在JSP标记中使用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到这个问题重新导入与之相关的js文件标签内容本身。我有一个类似的问题,这里我有一个生成一些HTML的jsp标签,并有一个通用的js实现来处理这个HTML的行为。此外,我需要编写一些初始化语句,因此我可以通过JavaScript使用它。为了能够在我的JavaScript中使用这个处理程序,它应该以某种方式可访问。

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 has a generic js-implementation that handles the behavior of this HTML. Furthermore I need to write some initialization statements, so I can use it afterwards through JavaScript. To be possible to use this "handler" within my JavaScript, it should be somehow accessible.

问题是......编写内联< script>是否可以。标签以及我的HTML用于实例化和初始化目的(我个人认为它不是很优雅)?关于JS世界的可访问性,我是否应该保留一个全局变量引用我的处理程序对象(我认为不是很优雅),有更好的方法吗?

The question is... Is it Ok to write inline <script> tags along with my HTML for instantiation and initialization purposes (personally I don't think its very elegant)? And about being accessible to the JS world, should I leave a global var referencing my handler object (not very elegant aswell I think), are there better ways to do it?

推荐答案

你应该在自己的文件中争取使用javascript。这通常通过渐进增强来完成。但有些时候你没有选择,例如当同一个JSP呈现不同语言的页面时。这是一个真实的例子:

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:

JSP:

  <script src="/javascript/article_admin.js"></script>  
  <script type="text/javascript">  
      NP_ArticleAdmin.initialize({  
            text: {  
              please_confirm_deletion_of: '<i18n:output text="please.confirm.deletion.of"/>',  
              this_cannot_be_undone: '<i18n:output text="this.cannot.be.undone"/>'  
            }  
      });  
  </script>  

javascript(article_admin.js):

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);

在这个例子中,JSP文件中唯一的javascript是需要存在的部分。核心功能在其自己的js文件中分开。

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.

这篇关于在JSP标记中使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆