doy2255 2016-09-23 04:29
浏览 56
已采纳

Ajax加载内联javascript

Is there a way to get AJAX to successfully load inline script functions?

I can't seem to figure out why this is not working. I am attempting to render a page with AJAX and the page render successfully; however, unless I declare a javascript function in the parent document, I cannot get the inline script to work. I generated a simple example that is relative to what I am attempting to accomplish.


PARENT DOCUMENT
    <!DOCTYPE html>
    <html>
      <head>
        <script type='text/javascript'>
          function loadChild(){
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
              if (this.readyState == 4 && this.status == 200) {
               document.getElementById("childContent").innerHTML = this.responseText;
              }
            };
            xhttp.open("GET", "./child.html", true);
            xhttp.send();
          }
        </script>
      </head>
      <body>
        <input type='button' value='Load Child' onclick='loadChild();'/>
        <div id='childContent'>

        </div>
      </body>
    </html>


CHILD DOCUMENT
    <input type='button' value='Child Function' onclick='childFunction();'/>
    <script type='text/javascript'>
      function childFunction(){
        alert('Do something');
      }
    </script>


I am trying to create an adaptive framework via PHP that I can use for my whole site and call the sub-sites using AJAX to load; however, to get this to currently function correctly, I am having to render every function in the parent document prior to calling each page. I want to know if there is a way to get AJAX to successfully load inline script functions?
  • 写回答

1条回答 默认 最新

  • duanchuonong5370 2016-09-23 05:28
    关注

    I such cases I use two techniques to do this,

    1. Using jQuery to use getscript() function to load a new script, which will automatically execute the script.

    For Example

    $.getScript( "child.js", function( data, textStatus, jqxhr ) {
        //add this script to the document
    });
    

    Here only child.js is loaded, it is then added to the document DOM (Script DOM) and it executes the javascript functions written inside the child.js file.

    1. Another way to do this is to add this whole code to DOM. JavaScript inserted as DOM text will not execute. But you can use dynamic script pattern to execute the new Javascript code.

    Please refer to this question in stack overflow: Javascript Inserted inside DOM. You will required to use eval() function in your javascript inside child.html code for DOM to be executed

    Thanks and best luck :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题