在firefox下就可以用script = doc.createElement("script"); script.innerHTML="function test(a,b,c){alert('...');}";head.appendChild(script);的方法.但是在IE下行不通。麻烦各位考虑下谢谢。。

解决方案 »

  1.   

    var aa= "function test(a,b,c){alert('...');}"
    eval(aa);
    test();
      

  2.   

    关键是事先我是不知道函数名是test..这个函数是我从其他地方用responseText得到的..页面也是动态写上去的..
      

  3.   

    <script>
    var aa 
    var fname = "ttt"
     function createfun(){
      aa = "function "+fname+"(a,b,c){alert('...');}"
     }
    createfun()
    eval(aa);
    eval(fname+"()");
    </script>你可以修改fname的值
      

  4.   

    多谢,好象页面的加载做到了..但是页面div..onclick="test();"的时候,还是提示找不到
    函数..不过还是谢谢,总算有点进展了.
      

  5.   

    比如我刚刚eval(fname+"()");的时候页面是弹出提示框..但是我想要的是..当我点页面div元素的时候..才去调用该函数..
      

  6.   

    div写成这样:
    <div onclick="eval(fname+'()');">sdfjioweow</div>
      

  7.   

    这样跟你说吧页面的div我也是responseText得到的.是body.innerHTML="";的方法
    加载进去的..意思很简单就是把另外一个页面的js,css,html,全部动态加载到当前的
    页面下.现在只有js的加载问题了.也就是说事先,我不知道页面元素..
      

  8.   

    loadHtml=function(textDoc) {
    var remoteScripts=[];
    var head = document.getElementsByTagName("head")[0];
    var htmlBody="";
    var body="";
    var styles=[]; var links=[];var match=[];
    while(match){
    match = textDoc.match(/<script([^>]*)>([\s\S]*?)<\/script>/i);
    if(!match){ break; }
    if(match[1]){
    attr = match[1].match(/src=(['"]?)([^"']*)\1/i);
    if(attr){
    var script=document.createElement("script");
    script.type="text/javascript";
    script.src=attr[2];
    head.appendChild(script);
    }
    }
    if(match[2]){
    if(navigator.appName.indexOf("Microsoft")==-1){
    var script=document.createElement("script");
    script.type="text/javascript";
    script.innerHTML=match[2];
    head.appendChild(script);
    }else{
    //在IE下的方法
    } }
    textDoc = textDoc.replace(/<script[^>]*>[\s\S]*?<\/script>/i, "");
    }
    //---加载页面
    var otherDiv=document.createElement("div")
    var matches = textDoc.match(/<body[^>]*>([\s\S]*?)<\/body>/im);
    if(matches) { body = matches[1]; }
    otherDiv.innerHTML+=body;
    htmlDiv.appendChild(otherDiv);
    }