1

I'm looking for a way to know the last event fired by an external object ( HTML tag).

The problem is that when I put some code in the page to handle specific event through the HTML tag <script for="" event="">handler()</script>, the event listener function(handler()) get also executed whenever the script tag is loaded into the page and not only when the specific event is fired.

The window.event variable is always null if checked during the event handler execution.

Test environment: IE10

Cheers, Andrea


You could do this

  1. Add the keyDown callback to the input (I assume it is an HTMLInputElement)

  2. In the keyDown callback delay the call to searchNameFromInput function so that the value of the input has already change when you check it.

  3. Filter elements in nameArray by creating a regex that checks for matches of names with your input value from the beginning (for instance: "an" would match "anivia" and "annie")

The code looks like this

var nameArray = ["aatrox","ahri","akali","alistar","amumu","anivia","annie","ashe", "blitzcrank",   "brand",    "caitlyn",  "cassiopeia",   "cho'gath", "corki",    "darius",   "diana",    "draven",   "dr. mundo",    "elise",    "evelynn",  "ezreal",   "fiddlesticks", "fiora",    "fizz", "galio",    "gangplank",    "garen",    "gragas",   "graves",   "hecarim",  "heimerdinger", "irelia",   "janna",    "jarvan iv",    "jax",  "jayce",    "karma",    "karthus",  "kassadin", "katarina", "kayle",    "kennen",   "kha'zix",  "kog'maw",  "leblanc",  "lee sin",  "leona",    "lissandra",    "lucian",   "lulu", "lux",  "malphite", "malzahar", "maokai",   "master yi",    "m.fortune",    "mordekaiser",  "morgana",  "nami", "nasus",    "nautilus", "nidalee",  "nocturne", "nunu", "olaf", "orianna",  "pantheon", "poppy",    "quinn",    "rammus",   "renekton", "rengar",   "riven",    "rumble",   "ryze", "sejuani",  "shaco",    "shen", "shyvana",  "singed",   "sion", "sivir",    "skarner",  "sona", "soraka",   "swain",    "syndra",   "talon",    "taric",    "teemo",    "thresh",   "tristana", "trundle",  "tryndamere",   "twitch",   "twisted fate", "udyr", "urgot",    "varus",    "vayne",    "veigar",   "vi",   "viktor",   "vladimir", "volibear", "warwick",  "wukong",   "xerath",   "xin zhao", "yorick",   "zac",  "zed",  "ziggs",    "zilean",   "zyra", "jinx"],
input = document.getElementById('foo');

// I also assume IE9+ for addEventListener
input.addEventListener('keydown', function keyDown(e) {
    e = e || window.event;
    // just bind the input element as a parameter
    // for the function searchNameFromInput
    setTimeout(searchNameFromInput.bind(null, e.target||e.srcElement), 80);
}, false);

function searchNameFromInput(input) {
    if (input.value.length) {
         console.log(
            nameArray.filter(function (name) {
                return new RegExp('^' + input.value).test(name);
            })
        );
    }
}
4

1 回答 1

1

您应该以不同的方式注册事件:

<script>document.getElmentById("SomeElement").addEventListener("click", handler);</script>

这将阻止您的函数handler()在浏览器解析您的脚本标签时执行。

于 2014-02-07T09:45:20.443 回答