[英]Firefox not executing JavaScript when it should
I've coded a very basic page with a search text input with ajax auto completion. 我编写了一个非常基本的页面,其中包含带有ajax自动完成功能的搜索文本输入。 I've set the input onchange attribute to call an ajax function that will retrieve a set a values from the server, and add options to the associated datalist. 我已经设置了输入onchange属性来调用ajax函数,该函数将从服务器检索一组值,并向相关的datalist添加选项。 When i change something in the text input, the JavaScript just doesn't execute. 当我在文本输入中更改某些内容时,JavaScript就不会执行。 However, when i minimize the window or click on a new tab, i does execute it, so it's neither a problem of syntax nor file inclusion i suppose. 但是,当我最小化窗口或单击新选项卡时,我确实执行它,因此它既不是语法问题也不是文件包含问题。 I also tried to clear my cache by it had no effect (and my firefox is totally cleared every time i close it anyway). 我也尝试通过它来清除我的缓存没有任何效果(并且每次关闭它时我的firefox都被完全清除)。 What could this be due to ? 这可能是什么原因? I'll paste my source code : 我将粘贴我的源代码:
My AJAX class. 我的AJAX课程。 It does work, i tested it at work, and it works fine. 它确实有效,我在工作中测试它,它工作正常。 Don't need to read it, i just paste it in case you want to have a look. 不需要阅读它,我只是粘贴它,以防你想看看。
/**
* @file ajax.js
* @version 1.0 - 2012-07-30
* @author *****
* @brief This file describes a class allowing to easily perform AJAX requests.
*/
/**
* @class Ajax
* @brief Used to performs AJAX requests.
* @details Usage example :
*
* var request1 = new Ajax()
* .url('some_url')
* .parameters('some_parameters')
* .callback(function(xhr) { alert(xhr.responseText); })
* .method('GET')
* .execute();
*
* var request2 = new Ajax();
* request2.url('some_url');
* request2.parameters('some_parameters');
* request2.callback(function(xhr) { alert(xhr.responseXml); });
* request2.execute();
*
* Using setters is not necessary, properties can be set manually. Setters only allow to chain calls by returning this.
* The order in which properties are set does not matter as long as the mandatory ones are set before calling 'execute'.
* If the server returns a string it can be retrieved from xhr.responseText. If it returns an XML file, it will be in
* xhr.responseXml.
*/
function Ajax()
{
/**
* @brief (string) URL of the request (MANDATORY).
* @details It main contain parameters, but if so, the property 'parameters' should be used instead.
*/
var url;
/**
* @brief (string) Method to use for the request.
* @details Can be either 'GET' or 'POST'.
*/
var method = 'POST';
/**
* @brief Function to be called once the request has been performed.
* @details This callback function will be given the XMLHttpRequest object as a parameter.
*/
var callback;
/**
* @brief Parameters of the URL.
* @details Must be in the following format : 'arg1=value1&arg2=value2'... and contain no question mark.
*/
var parameters;
/**
* @brief Indicates if the request is syncrhonous.
*/
var wait = false;
/**
* @brief Sets the 'url' property.
* @details Returns the object to chain setter calls.
* @param String.
* @return Ajax.
*/
this.url = function($url) { url = $url; return this; };
/**
* @brief Sets the 'method' property.
* @details Returns the object to chain setter calls.
* @param String.
* @return Ajax.
*/
this.method = function($method) { method = $method; return this; };
/**
* @brief Sets the 'callback' property.
* @details Returns the object to chain setter calls.
* @param Function.
* @return Ajax.
*/
this.callback = function($callback) { callback = $callback; return this; };
/**
* @brief Sets the 'parameters' property.
* @details Returns the object to chain setter calls. WARNING : Using parameters will change the method to POST. If
* parameters must be passed using GET, they must be included in the URL.
* @param String.
* @return Ajax.
*/
this.parameters = function($parameters) { parameters = $parameters; method = 'POST'; return this; };
this.wait = function($wait) { wait = $wait; return this; };
// FIXME POST semble poser probleme lorsque l'on renvoie du json et que l'on l'éval.
/**
* @brief Performs the AJAX request.
* @details The mandatory properties must be set before calling this method.
*/
this.execute = function()
{
var xhr = null;
try { xhr = new XMLHttpRequest(); }
catch(e) {
try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e2) {
try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e) {}
}
}
var self = this;
xhr.onreadystatechange = function() {
if(4 === xhr.readyState && 200 === xhr.status) {
if(callback) return callback(xhr);
}
};
xhr.open(method, url, !wait);
if('POST' === method) xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send(parameters ? parameters : null);
return this;
};
return this;
}
My AJAX auto completion function : 我的AJAX自动完成功能:
function ajaxAutoComplete(inputId)
{
var input = document.getElementById(inputId);
if(null === input) alert('ajaxAutoComplete : invalid input ID.');
if(!input.list) alert('ajaxAutoComplete : input has no associated data list');
var list = document.getElementById(input.list.id);
if(null === list) alert('ajaxAutoComplete : input has an invalid list ID.');
var suggestions;
new Ajax()
.url('../Controller/index.php?exec=ajaxAutoComplete')
.parameters('searchInput=' + input.value)
.callback(function(xhr)
{
suggestions = eval('(' + xhr.responseText + ')');
while(list.hasChildNode) list.removeChild(list.firstChild);
for(var i = 0; i < suggestions.length; ++i)
{
var option = document.createElement('option');
option.value = suggestions[i];
list.appendChild(option);
}
})
.wait(false)
.execute();
} // ajaxAutoComplete()
And the HTML code : 和HTML代码:
<!DOCTYPE html>
<html>
<head>
<title>Image Module</title>
<meta charset="utf-8">
<script type="text/javascript" src="../Js/main_dev.js"></script>
</head>
<body>
<header>
<h1>Image module</h1>
</header>
<nav>
<form action="#" method="#">
<fieldset>
<label for="searchInput">Search</label>
<input id="searchInput" list="searchSuggestions" name="searchInput" type="text" value="search" maxlength="255" onchange="ajaxAutoComplete(this.id)">
<datalist id="searchSuggestions">
</datalist>
<input type="submit" value="Search">
</fieldset>
</form>
</nav>
<div class="Content">
</div>
<footer>
<span></span>
</footer>
</body>
</html>
The server returns a JSON encoded array of randomly generated numbers such as : [1611, 1515187, 415417, 7815, 587187] 服务器返回随机生成的数字的JSON编码数组,例如:[1611,1515187,415417,7815,587187]
I'm using Firefox 14. 我正在使用Firefox 14。
Any ideas ? 有任何想法吗 ? Thanks for reading ! 谢谢阅读 ! :) :)
The change
event doesn't get fired until the input loses focus. 在输入失去焦点之前,不会触发change
事件。 You might want to listen for keyup
or keydown
instead. 你可能想听一下keyup
或keydown
。
The change
event only fires when the input loses focus. 仅在输入失去焦点时才会触发change
事件 。
The change event occurs when a control loses the input focus and its value has been modified since gaining focus. 当控件失去输入焦点并且自获得焦点后其值已被修改时,会发生更改事件。 This event is valid for INPUT, SELECT, and TEXTAREA. 此事件对INPUT,SELECT和TEXTAREA有效。 element. 元件。
Bind to a different event. 绑定到另一个事件。
Also consider using existing code/ widgets / libraries /etc. 还要考虑使用现有的代码/ 小部件 / 库 /等。 – I don't really see what all this wheel-reinventing is supposed to gain you aside from lost time. - 我真的没有看到所有这些轮子重新发明应该让你除了浪费时间。
I'd suggest not making an AJAX call for each character pressed. 我建议不要为每个按下的字符进行AJAX调用。 Delay the request for a few moments to see if the user is still typing. 将请求延迟片刻以查看用户是否仍在键入内容。 Otherwise you might end up with a previous letter's json being parsed as though it is the current. 否则你最终可能会解析前一个字母的json被解析,就好像它是当前的一样。 To do this just make a simple queue: 为此,只需创建一个简单的队列:
<input id="searchInput" list="searchSuggestions" name="searchInput" type="text" value="search" maxlength="255" onkeydown="queueRequest(this.id)">
Your Javascript would then look like 你的Javascript会是这样的
function queueRequest(id) {
if (queuedRequest) clearTimeout(queuedRequest);
window.queuedRequest = setTimeout(function(id) {
ajaxAutoComplete(id);
},100,id);
}
A few notes, this will not work with IE8 or lower due to lack of support for the 3rd argument of setTimeout 一些注意事项,由于缺乏对setTimeout的第3个参数的支持,因此无法使用IE8或更低版本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.