栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在通过ajax加载的html中运行javascript

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在通过ajax加载的html中运行javascript

如果要按需加载Javascript。这可以通过动态创建脚本标签来完成。Stoyan
Stefanov
书-Javascript
Patterns中说明了这种模式

这是从书中摘录的:

编写一个require函数。然后这样称呼它:

require("extra.js", function () {    functionDefinedInExtraJS();});

样品要求功能:

function require(file, callback) {    var script = document.getElementsByTagName('script')[0],        newjs = document.createElement('script');    // IE    newjs.onreadystatechange = function () {        if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') { callback();        }    };    // others    newjs.onload = function () {        callback();    };    newjs.src = file;    script.parentNode.insertBefore(newjs, script);}


http://www.jspatterns.com/book/8/ondemand.html中找到的实时示例

@Edit:针对您的情况的更多详细信息。我将尝试使事情变得简单:

创建fours文件:

  • index.php:测试文件。
  • pre.js:具有Ajax,getJS,getHTML函数的实际代码
  • content.php:任何可以打印纯HTML而无需任何JS的PHP文件
  • content.js:要动态运行的javascript代码。

index.php

<html>    <head>        <script src="pre.js"></script>    </head>    <body>        <input type="button" onclick="Ajax('content.php', 'd_html');" value="Fill from content.php"/>        <div id="d_html"></div>        <br>        <input type="button" onclick="Ajax('content.js', 'd_js');" value="Fill from content.js"/>        <div id="d_js"></div>    </body></html>

content.php

<span>Hello, I am dynamic span came from content.php</span>

content.js

//Ana javascript pre you want to run it by Ajax function should go inside this functionfunction executeJS(element){   element.innerHTML = "<span>Hello, I am dynamic span came from content.js</span>";}

pre.js

//This function responsible for doing the ajax request for any file that will return pure HTML.function getHTML(url, element){    var i, xhr, activeXids = [        'MSXML2.XMLHTTP.3.0',        'MSXML2.XMLHTTP',        'Microsoft.XMLHTTP'    ];    if (typeof XMLHttpRequest === "function") { // native XHR        xhr =  new XMLHttpRequest(); } else { // IE before 7        for (i = 0; i < activeXids.length; i += 1) { try {     xhr = new ActiveXObject(activeXids[i]);     break; } catch (e) {}        }    }    xhr.onreadystatechange = function () {        if (xhr.readyState !== 4) { return false;        }        if (xhr.status !== 200) { alert("Error, status pre: " + xhr.status); return false;        }        element.innerHTML += xhr.responseText;    };    xhr.open("GET", url, true);     xhr.send("");}//This function will load javascript file on-demand and call executeJS function inside that file.function getJS(url, element, cb){    var newjs = document.createElement('script');    // IE    newjs.onreadystatechange = function () {        if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') { cb();        }    };    // others    newjs.onload = function () {        cb();    };    newjs.src = url;    element.appendChild(newjs);}//This is same as your function, but now can handle both PHP and JS filesfunction Ajax(url, id){    var element = document.getElementById(id),        regex = /.js$/;    if(!element){        alert("Invalid ID");        return false;    }    if(regex.test(url)){ //If url ends with JS, load using getJS        getJS(url, element, function(){ executeJS(element);        });    } else {        getHTML(url, element);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616651.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号