Frage

I'm trying to write a simple script that will take a list of urls that I need to save, append them to an array, and then spit that array back out in a div. The problem I'm having is that I am using ".onKeyUp" to call the function. It works, but when I use ctrl + v to paste, the function gets called twice. Once when you release the letter v and once when you release control. How can I fix it so that it will only call the function once when I'm using the keyboard shortcut? Here is my code:

<script type="text/javascript">
    var arr = new Array();
    function procession() {
        var urlin = document.getElementById('url').value;
        var smacky = "<br/> the url is:    " + urlin + "<br/> the url is:    www." + urlin;
        arr.push(smacky);
        document.getElementById('list').innerHTML = arr;
    }
</script>


<form>
    <input onkeyup="procession();" type="text" size="50" id="url"/>
</form>
<div id="list"></div>

Important Note: It's a long story, but I can't use jQuery for this script. Is there a way to do it in just pure javascript?

War es hilfreich?

Lösung

Pass in event to your onKeyUp, if the key is a ctrl, return false. This will only register one keyup event for shortcuts now:

<input onkeyup="procession(event);" type="text" size="50" id="url"/>

JS:

function procession(e) {
    //if ctrl key, return false
    if (e.which == 17) return false;

    //do stuff
    var urlin = document.getElementById('url').value;
    var smacky = "<br/> the url is:    " + urlin + "<br/> the url is:    www." + urlin;
    arr.push(smacky);
    document.getElementById('list').innerHTML = arr;
}

It's a little hack-ish, but it should get the job done.

Andere Tipps

Here:

$(document).on("paste", function(event) {
  alert("paste!");
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top