执行JavaScript通过AJAX加载加载、JavaScript、AJAX

2023-09-11 01:26:19 作者:2018世纪劳资称霸ぴ

可能重复:   How你执行一个动态加载的JavaScript块?

我看过很多的问题,比如我的这里,但我没有发现这是装修给我一个答案。

I have seen many questions like mine here, but I didn't find an answer which is fitting for me.

我通过AJAX加载code,还包括剧本标签包括JavaScript。正如我已经找到了,这个JavaScript不执行。

I'm loading Code via AJAX, also including a script Tag including Javascript. As I already found out, this Javascript is not executed.

我还发现,的eval()可以帮我,但我是一个JavaScript小白,只需要它这一次,我也没办法哪儿把它。

I also found out that eval() can help me, but as I'm a noob in Javascript and just need it for this one time, I have no idea where exactly to put it.

我的PHP脚本返回一个字符串,我分裂与Javascript来把它放到页面的不同部分。这是工作的罚款。一个部件的组成是:

My PHP script is returning a string which I split with Javascript to put it into different parts of the page. This is working fine. One of the parts consists of this:

<div id=\"fb-root\"></div> <script>(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return;js = d.createElement(s); js.id = id;js.src = \"//connect.facebook.net/de_DE/all.js#xfbml=1\";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script>

包括像这样的:

Included like this:

document.getElementById("id").innerHTML=response1;

其中, response1 是我已经把code从上面分割后的字符串变量。任何人能帮助我的小白,简单的方法?

Where response1 is the variable where I have put the code from above after splitting the string. Anyone able to help me in a noob, easy way?

推荐答案

您可以使用eval()。你必须给一个ID为你的脚本:

You can use eval(). You have to give an ID to your script:

<script id='script'>
     some javascript
</script>

再经过加载你的Ajax响应,加载脚本添加此行:

And then after loading your ajax response, for loading the script you add this line:

eval(document.getElementById('script').innerHTML);

祝你好运!

 
精彩推荐
图片推荐