通过AJAX通过回声在PHP中执行的JavaScript, [英] executing javascript in PHP through echo, via ajax

查看:94
本文介绍了通过AJAX通过回声在PHP中执行的JavaScript,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我建立一个用户登录系统,它具有诠释EH HTML页面一个div容器和JavaScript文件。在JavaScript文件,它使AJAX调用不同的PHP网页,然后回的HTML code的表单中选择,然后将其插入DIV容器。

so i build a login user system that has int eh html page a div container and a javascript file. in the javascript file, it makes ajax calls to different php pages that then echo back the html code for the form selected that is then inserted into the div container.

(这样的网页不刷新 - 它的动态内容,如果有人抓住源头,他们所得到的是js文件和一个div容器)

(Thus the page does not refresh - its dynamic content. and if someone grabs the source, all they get is the js file and a div container)

我的问题NW是我需要在echo'd HTML在PHP是通过AJAX调用运行一些JavaScript代码。

my problem nw is i need to run some javascript in the echo'd html in the php that is being called via ajax.

到PHP这样的AJAX调用,PHP回声一个字符串和AJAX然后执行一个的document.getElementById(容器)的innerHTML = xmlhttp.responseText;

so ajax call to php, php echos a string and the ajax then performs a document.getElementById("Container").innerHTML=xmlhttp.responseText;

PHP的codeI有有2个输入字段,我需要它的元素.focus ONT他第一个。所以我有这样的事情是echod'd:

The php code i have has a 2 input fields, and i need it to element .focus ont he first one. so i have something like this being echod'd:

echo "

<form>
<input name='username' type='text' id='username'>
</form>

<script type='text/javascript'>
document.getElementById('username').focus();
</script>

";

你猜怎么着,这是行不通的.. 请帮忙

And guess what, it does not work.. Please help

推荐答案

(这样的网页不刷新 - 它的动态内容,如果有人抓住源头,他们所得到的是js文件和一个div容器。 )

他们仍然可以很容易地得到完整的源代码,通过使用萤火虫(Firefox插件)或其他工具。你不能保护您的源$ C ​​$ C这个样子。

They can still easily get the full source, by using firebug (firefox add-on) or some other tool. You can't protect your source code this way.

理念#1: 使用DOM获得Ajax响应,则追加新的JavaScript文档的头部。

Idea #1: Use DOM to get the AJAX response, then append the new javascript to the head of the document.

理念#2: 使用JS框架获得响应,更容易。我知道jQuery都会执行Ajax响应脚本(getScript加入脚本功能,链接)。这是一个问题的答案做相同的<一个href="http://stackoverflow.com/questions/278122/how-to-force-javascript-to-execute-within-html-response-to-ajax-request">Prototype框架。

Idea #2: Use a js framework to get the response, much easier. I know jQuery executes the script in AJAX response (getScript function, link). Here is a question with answers to do the same with Prototype framework.

理念#3: 使用eval()。这被认为是不好的做法,但:

Idea #3: Use eval(). This is not considered good practice though:

eval(document.getElementById('username').focus());

这篇关于通过AJAX通过回声在PHP中执行的JavaScript,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆