I have a system that loads a div with content through the use of AJAX. All is well, I can get the div to load perfectly with details from my database and it works great.
我有一个系统,通过使用AJAX加载包含内容的div。一切都很好,我可以让div完全加载我的数据库中的细节,它的效果很好。
The problem is this:
问题是这样的:
Inside the returned div, I have a couple of buttons with onClick
events. The onClick
events cause an error when I attempt to call a user defined function onClick="my_function();"
but the onClick
events do not cause an error when I call a JS defined function onClick="alert('success');"
.
在返回的div中,我有几个带onClick事件的按钮。当我尝试调用用户定义函数onClick =“my_function();”时,onClick事件会导致错误但当我调用JS定义函数onClick =“alert('success');”时,onClick事件不会导致错误。
Anyone know why this would be? my_function()
is defined in the head of my page. Although, I have also tried to define it in the content returned by AJAX, still with no luck.
谁知道为什么会这样? my_function()在我的页面的头部定义。虽然,我也尝试在AJAX返回的内容中定义它,但仍然没有运气。
Any help will be greatly appreciated
任何帮助将不胜感激
4 个解决方案
#1
if you are using prototype, jquery, etc.. (any JS framework), you need to be sure you have set the param "evalJS" to true. this last example I put works in prototype.
如果你正在使用prototype,jquery等...(任何JS框架),你需要确保你已经将参数“evalJS”设置为true。最后一个例子我把作品放在原型中。
check here for prototype
在这里查看原型
#2
You could try adding event handlers via JavaScript instead of onclick handlers on the actual elements.
您可以尝试通过JavaScript而不是onclick处理程序在实际元素上添加事件处理程序。
Then you could use anonymous functions.
然后你可以使用匿名函数。
#3
If you're simply updating the innerHTML
of a DIV
element with the result of an AJAX request, keep in mind that the browser parses only HTML not scripts.
如果您只是使用AJAX请求的结果更新DIV元素的innerHTML,请记住浏览器仅解析HTML而不是脚本。
Probably that's why you're recieving the JavaScript Error.
可能这就是你收到JavaScript错误的原因。
To parse also the scripts you need to consult the documentation of your library. For instance, if you're using ASP.NET you can call ScriptManager.RegisterClientScriptBlock
in order to instruct the AJAX client componenet to parse the scripts in the result.
要解析您需要查阅库的文档所需的脚本。例如,如果您使用的是ASP.NET,则可以调用ScriptManager.RegisterClientScriptBlock以指示AJAX客户端组件解析结果中的脚本。
On the other hand, if you're doing it by hand (one thing I did awhile back), you need to detect and parse the scripts yourself.
另一方面,如果你手工完成(有一件事我做了一段时间),你需要自己检测和解析脚本。
#4
OK, here is the simple answer.. I had the button element name and id as the same as the function which it was trying to call.. stupid mistake I know.. anyhow, it is now working.. however, I do have another problem.. my ajax only fires once.. it works great the first time, but after that the function doesn't work
好的,这是简单的答案..我的按钮元素名称和id与它试图调用的函数相同..愚蠢的错误我知道..无论如何,它现在正在工作..但是,我确实有另一个问题..我的ajax只发射一次..它第一次工作很棒,但之后功能不起作用
here is my code:
这是我的代码:
function delete_account(account_id) {
create_request();
var url = "accounts_ajax.php?action=delete_account&account_id=" + account_id;
show_div('action_window');
document.getElementById('action_window').innerHTML = '<div class="please_wait"><img src="images/working.gif"><br>Loading...</div>';
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function(){if(xmlHttp.readyState!=4)return;if(xmlHttp.status==200){reload_div('action_window', xmlHttp.responseText);}};
xmlHttp.send(null);
}
it just causes an error second time around.
它只是第二次导致错误。
#1
if you are using prototype, jquery, etc.. (any JS framework), you need to be sure you have set the param "evalJS" to true. this last example I put works in prototype.
如果你正在使用prototype,jquery等...(任何JS框架),你需要确保你已经将参数“evalJS”设置为true。最后一个例子我把作品放在原型中。
check here for prototype
在这里查看原型
#2
You could try adding event handlers via JavaScript instead of onclick handlers on the actual elements.
您可以尝试通过JavaScript而不是onclick处理程序在实际元素上添加事件处理程序。
Then you could use anonymous functions.
然后你可以使用匿名函数。
#3
If you're simply updating the innerHTML
of a DIV
element with the result of an AJAX request, keep in mind that the browser parses only HTML not scripts.
如果您只是使用AJAX请求的结果更新DIV元素的innerHTML,请记住浏览器仅解析HTML而不是脚本。
Probably that's why you're recieving the JavaScript Error.
可能这就是你收到JavaScript错误的原因。
To parse also the scripts you need to consult the documentation of your library. For instance, if you're using ASP.NET you can call ScriptManager.RegisterClientScriptBlock
in order to instruct the AJAX client componenet to parse the scripts in the result.
要解析您需要查阅库的文档所需的脚本。例如,如果您使用的是ASP.NET,则可以调用ScriptManager.RegisterClientScriptBlock以指示AJAX客户端组件解析结果中的脚本。
On the other hand, if you're doing it by hand (one thing I did awhile back), you need to detect and parse the scripts yourself.
另一方面,如果你手工完成(有一件事我做了一段时间),你需要自己检测和解析脚本。
#4
OK, here is the simple answer.. I had the button element name and id as the same as the function which it was trying to call.. stupid mistake I know.. anyhow, it is now working.. however, I do have another problem.. my ajax only fires once.. it works great the first time, but after that the function doesn't work
好的,这是简单的答案..我的按钮元素名称和id与它试图调用的函数相同..愚蠢的错误我知道..无论如何,它现在正在工作..但是,我确实有另一个问题..我的ajax只发射一次..它第一次工作很棒,但之后功能不起作用
here is my code:
这是我的代码:
function delete_account(account_id) {
create_request();
var url = "accounts_ajax.php?action=delete_account&account_id=" + account_id;
show_div('action_window');
document.getElementById('action_window').innerHTML = '<div class="please_wait"><img src="images/working.gif"><br>Loading...</div>';
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function(){if(xmlHttp.readyState!=4)return;if(xmlHttp.status==200){reload_div('action_window', xmlHttp.responseText);}};
xmlHttp.send(null);
}
it just causes an error second time around.
它只是第二次导致错误。