简体   繁体   English

使用ajax在加载的页面中运行javascript

[英]Running javascript in loaded page with ajax

I read in another thread that I will be able to put javascript codes in page y and it will work in page x when loaded with ajax. 我读到另一个线程,我将能够在y页中放置javascript代码,并且在加载ajax时将在x页中工作。 I didnt get an error but it did not work either. 我没有收到错误,但是也没有用。

Running javascript in page that is loaded with jQuery Ajax 在jQuery Ajax加载的页面中运行javascript

    $(function CheckinMap() {
        $.ajax({
            dataType: "html",
            url: "content/home/index.cs.asp?Process=ViewCheckinMap",
            success: function(data) {
                //$("#friends").html(data);
                $("#checkinmap").append(data);

            },
            error: function (data) {
                $("#checkinmap").append(data);
            }
        });
    });

What you're looking for is .load() . 您正在寻找的是.load() This will load all the JS, instead of adding it to the page as dead text. 这将加载所有JS,而不是将其作为无效文本添加到页面中。

您可以使用getScript API: http ://api.jquery.com/jQuery.getScript/

In the content that you are injecting via AJAX you can do an anonymous function call to have it execute after its added to the DOM 在通过AJAX注入的内容中,您可以执行匿名函数调用以使其在添加到DOM后执行

<script language="javascript">

(function doStuff(){ alert('hello, I was injected'); })();

</script>

That has worked for me in the past.... 过去对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM