重定向时反复触发IE10 oninput事件

IE10 oninput event repeatedly triggered on redirection

本文关键字:IE10 oninput 事件 重定向      更新时间:2023-09-26

我在IE10中遇到了一个奇怪的问题,当在'oninput'事件上重定向页面时(使用Chrome时没有类似的问题)。我已经生成了仍然显示问题的代码的最精简版本,如下所示:

<html>
<head>
<script type="text/javascript">
function onChangeInputText()
{
    window.location.href = "oninput_problem.html"; // Back to this page.
}
</script>
</head>
<body>
<input type="text"
    oninput="onChangeInputText()"
    value="£"
    />
</body>
</html>

在我使用IE10的Windows 7 PC上,这段代码在浏览时(通过双击或通过Apache)反复重定向,好像初始化输入文本框本身的值的行为正在立即生成'oninput'事件。但是,当我将初始输入文本框的值从'£'符号更改为字母' a '时,重复的重定向不会发生。

我第一次注意到这个问题是在我正在做的一个项目的一部分。在这种情况下,用户的输入应该导致延迟的页面刷新,当我输入"£"符号时,页面开始反复重定向。正如我所说,上面的代码是我在试图跟踪导致问题的原因时产生的最精简的版本。

有人在使用IE10时遇到过这种情况吗?如果是这样,谁能解释一下为什么IE10是这样的?

我发现了以下显示这可能是IE10的一个bug:

social.msdn.microsoft.com:页面加载触发oninput事件

还有一个后续的bug报告(在上面链接的页面中):

connect.microsoft.com: onInput事件在加载页面时触发当输入@value是非ascii

编辑补充:在第二个链接指向的页面底部,有一个似乎是来自微软的无用回复,声明他们无法重现所描述的错误,因此问题可能需要一段时间才能修复…

还有一个bug报告还在打开:

http://connect.microsoft.com/IE/feedback/de..。

对于可能遇到这种情况的人,您可以使用这个"类"作为onInput事件的替代。

const getFieldWatcherInstance = (function(watchedElement, onElementValueChange){
        let intervalReference = null;
        let lastValue = null;
        if(!watchedElement instanceof Element) return new Error('watchedElement is not an HTML Element');
        if(typeof onElementValueChange !== 'function') return new Error('onElementValueChange is not a function');
        return {
            toggleFieldWatching : function(){
                if(intervalReference){
                    clearInterval(intervalReference);
                    intervalReference = null;
                }
                else{
                    intervalReference = setInterval(function(){
                        const currentValue = watchedElement.value;
                        if(lastValue !== currentValue){
                            onElementValueChange(currentValue);
                            lastValue = currentValue;
                        }    
                    }, 100);   
                }    
            }
        };    
    });

设置如下:

const myInputChangeWatcher = getFieldWatcherInstance(<**insert real element reference here**>, function(newValue){
console.log('The new value is: ' + newValue);
});

在输入

的onfocus和onblur事件中调用myinputchangeobserver . togglefieldwatching ()