执行后AJAX加载页面的JavaScript脚本

执行后AJAX加载页面的JavaScript脚本

本文介绍了执行后AJAX加载页面的JavaScript脚本 - 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个网页与AJAX,但是当我得到的页面,它包括了JavaScript code - 它不执行它

I'm trying to get a page with AJAX, but when I get that page and it includes Javascript code - it doesn't execute it.

为什么?

简单code在我的Ajax页面:

Simple code in my ajax page:

<script type="text/javascript">
alert("Hello");
</script>

...和它不执行它。我尝试使用谷歌地图API和AJAX添加标记,所以每当我添加一个我执行一个AJAX页面,获取新的标记,并将其存储在数据库中,并应标记动态添加到地图中。

...and it doesn't execute it. I'm trying to use Google Maps API and add markers with AJAX, so whenever I add one I execute a AJAX page that gets the new marker, stores it in a database and should add the marker "dynamically" to the map.

但因为我无法执行单一的JavaScript函数就这样,我该怎么办?

But since I can't execute a single javascript function this way, what do I do?

是我在页面上已经预先定义的保护或我的私人的功能?

Is my functions that I've defined on the page beforehand protected or private?

**更新了AJAX功能**

** UPDATED WITH AJAX FUNCTION **

function ajaxExecute(id, link, query)
{
    if (query != null)
    {
        query = query.replace("amp;", "");
    }

    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            if (id != null)
            {
                    document.getElementById(id).innerHTML=xmlhttp.responseText;
            }
        }
    }

    if (query == null)
    {
        xmlhttp.open("GET",link,true);
    }
    else
    {
        if (query.substr(0, 1) != "?")
        {
            xmlhttp.open("GET",link+"?"+query,true);
        }
        else
        {
            xmlhttp.open("GET",link+query,true);
        }
    }
    xmlhttp.send();
}

**由Deukalion解决方案**

** Solution by Deukalion **

var content = xmlhttp.responseText;

if (id != null)
{

    document.getElementById(id).innerHTML=content;
    var script = content.match("<script[^>]*>[^<]*</script>");

    if (script != null)
    {
        script = script.toString().replace('<script type="text/javascript">', '');
        script = script.replace('</script>', '');
        eval(script);

    }
}

和对某些事件,我不得不脚本的addEvent听众,而不是仅仅做一个内选择的onchange ='executeFunctionNotIncludedInAjaxFile();我不得不对addEventListener(变,函数名,假的)这一点。在脚本正在评估。

and on certain events, I had to within the script addevent listeners instead of just making a "select onchange='executeFunctionNotIncludedInAjaxFile();'" I had to addEventListener("change", functionName, false) for this. In the script that is being evaluated.

推荐答案

在做类似设置一个容器的innerHTML 来一些更新的内容更新网页,浏览器根本不会在它运行的脚本。你可以找到&LT;脚本&GT; 标签,让他们的的innerHTML (IE可能preFER 的innerText ),然后的eval()自己(脚本是pretty的多是什么jQuery不会,但它找到脚本与更新DOM)前一个正则表达式。

When you update your page by doing something like setting a container's innerHTML to some updated content, the browser simply will not run the scripts in it. You can locate the <script> tags, get their innerHTML (IE may prefer innerTEXT), and then eval() the scripts yourself (which is pretty much what jQuery does, though it finds the scripts with a regex before updating the DOM).

这篇关于执行后AJAX加载页面的JavaScript脚本 - 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 07:09