分享java带来的快乐

我喜欢java新东西

使用prototype的Ajax.Updater更新带javascript的html/Ajax中执行js的方法

 
使用prototype的Ajax.Updater更新带javascript的html/Ajax中执行js的方法
2009-09-10 17:23

prototype的Ajax.Updater可以用来更新一段html,但在更新带javascript的html时,需要注意几个问题

一. 需要在Ajax.Updater添加参数 evalScripts : true

二. javascript代码块要使用下面的形式
showHi = function() {
    alert('Hi');
}

而不能使用下面的形式
function showHi() {
    alert('Hi');
}

三. 注意javascript代码块中不能使用 <!-- //--> 标记,否则 IE 会出错,firefox没问题。

下面是个例子, 在a1.jsp中加载a2.jsp,

a1.jsp代码
<html>  
<head>  
<title>Insert title here</title>  
<script type="text/javascript" src="prototype-150.js"></script>  
<script type="text/javascript">  
function ajax() {   
     var url = "/MyWebApp/a2.jsp";   
     var myajax = new Ajax.Updater(   
         {success: 'mydiv'},   
         url,   
         {   
             asynchronous:true,   
             method: 'get',   
             evalScripts:true   
         }   
     );   
}   
</script>  
</head>  
<body>  
    <a href="#" onclick="ajax()">click here1</a>  
  
    <div id="mydiv"></div>  
</body>  
</html>  

a2.jsp代码
<script type="text/javascript">  
    showHi = function() {   
         alert('Hi');   
     }   
    sayHello = function() {   
         alert('Hello');   
     }   
</script>  
  
<a href="#" onclick="showHi();">showHi</a>  
  
<script type="text/javascript">  
sayHello();   
</script>  

posted on 2009-09-30 00:39 强强 阅读(771) 评论(0)  编辑  收藏 所属分类: web技巧


只有注册用户登录后才能发表评论。


网站导航: