2

I would like to use this script as a function :

jQuery(function ($) {
    var kkeys = [],
        login = '76,79,71,73,78'; //login
    $(document).keydown(function (e) {
        kkeys.push(e.keyCode);
        if (kkeys.toString().indexOf(login) >= 0) {
            $(document).unbind('keydown', arguments.callee);
            return hs.htmlExpand(this, {
                contentId: 'highslide-html-loginform',
                wrapperClassName: 'mod_cdlogin',
                outlineType: 'rounded-white',
                align: 'auto',
                anchor: 'auto',
                dimmingOpacity: 0,
                slideshowGroup: 'mod_cdlogin_loginform'
            })
        }
    });
});

So I could have this code in my js part a call a function in another file, for example codelogin('mycode') and 'mycode' would be 76,79,71,73,78 in that case. I tried many things but it's not working. The script as itself works fine, but I'm not used to work with jQuery so that might be my problem, I looked for a way to do it but I'm kind of lost. Any help would be appreciated.

2 Answers 2

1

You can put the code in function and call it from other script block of file.

<script>


jQuery(function($){

    function YourFunName(myCode){
    {
    var kkeys = [];     
    login = myCode;//login
    $(document).keydown(function(e)
    {
    kkeys.push( e.keyCode );
    if( kkeys.toString().indexOf( login ) >= 0 )
    {
    $(document).unbind('keydown',arguments.callee);
    return hs.htmlExpand(this, { contentId: 'highslide-html-loginform', wrapperClassName: 'mod_cdlogin', outlineType: 'rounded-white', align: 'auto', anchor: 'auto', dimmingOpacity: 0, slideshowGroup: 'mod_cdlogin_loginform' })
    }
    });
    }

     YourFunName('76,79,71,73,78');
});

</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Your code is nearly there. Here's one implementation I was able to work up that accepts a code, in this case the string "jonathan", a callback function and an optional element to operate on. By default the code will bind to the document object if no other option is requested:

// When 'jonathan' is entered, fire off alertSuccess
bindcode ("jonathan", alertSuccess);

// Our callback function
function alertSuccess () {
    alert("You did it!");
}

// The bindcode function takes a code, a callback, and an optional element
function bindcode( code, callback, element ) {
    var input = [];
    // When the keypress event occurs on either your element, or the document
    $(element || document).on("keypress", function(e){
        // Push the new character onto the input array
        input.push(String.fromCharCode(e.which));
        // Convert to a string and check for presence of code
        if (input.join("").indexOf(code) > -1) {
            // Unbind the keypress event, and fire off callback
            $(this).off("keypress");
            callback();
        }
    });
}

Demo: http://jsfiddle.net/rQU4A/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.