Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7707507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:22:41+00:00 2026-06-01T00:22:41+00:00

document.onkeydown.toString() returns onkeydown function but I only want to get this function’s name. Is

  • 0

document.onkeydown.toString() returns onkeydown function but I only want to get this function’s name.

Is it possible?

var exHandler;
function getCurrentHandler(){
   currentHandler = document.onkeydownhandler.toString() - i want to get name of this to call setPreviousHandler with exHandler

document.onkeydown = newFunction;

setPreviousHandler(exHandler);
}

function setPreviousHandler(targetHandler){
   document.onkeydown = targetHandler;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:22:42+00:00Added an answer on June 1, 2026 at 12:22 am

    It depends on the way the function was assigned.
    Take look at this:

    function test() { alert('test'); }
    window.onkeydown = test;
    window.onkeydown.toString(); 
    

    output is function test() { alert('test'); } and you can parse this string to get its name as string:

    var fnsting = window.onkeydown.toString(); 
    // use regexp to extract name
    var name = fnsting.match( /function\s*(.*?)\s*\(/ )[1]; // name is 'test'
    

    (it will allways returns name from function definition (when you assing this function to other variable .toString() always returns the same string)

    but following example:

    window.onkeydown = function () { alert('test'); };
    window.onkeydown.toString(); 
    

    will print function () { alert('test'); } because this function is anonymous, so you can’t get its name.

    Moreover when you create function like this:

    var test = function () { alert('test'); };
    window.onkeydown = test;
    window.onkeydown.toString(); 
    

    and output will be function () { alert('test'); } because on creation function did not have name (it was function expression assigned to variable) so you also can not extract it’s name.

    But you can make simple comparision:

    function foo() { alert('foo'); }
    var bar = function () { alert('bar'); };
    
    window.onkeydown = foo;
    
    // try to find function
    if (window.onkeydown === foo) {
        alert('this is foo');
    } else if (window.onkeydown === bar) {
        alert('this is bar');
    }
    
    // or like this:
    var handler = window.onkeydown;
    if (handler === foo) {
        alert('this is foo');
    } else if (handler === bar) {
        alert('this is bar');
    }
    

    But there is also issue with this, because when you assign anonymous function like this:

    window.onkeydown = function () { alert('test'); };
    

    You don’t have any other reference to this function (it is not assigned to other variable) so you can’t compare it with other function (comparision always returns false).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$(document).ready(function() { $(span.link).mouseover(function(e){ $(this.children).css(display,inline); }); }); I'm not a javascript expert, but I've cobbled
var input = document.getElementById(myInput); a.onkeydown = function() { if (a.value.match(hi)) { a.value.replace(hi, span style='color:red;'>hi</span>);
$(document).ready(function() { function countChecked() { var n = $(input:checked).length; $(div).text(n + (n <= 1
function test() { this.str = hello; this.sayHello = function() { document.write(this.str); } this.init =
This works in IE, but I cannot get it to work in Opera or
What does this: >> mean in JavaScript? Seen in this context: document.onkeydown = document.onkeyup
I've got the following code: document.onkeydown=function(e) { if (e.which == 13 && isCtrl) {
document.getElementById('container').addEventListener('copy',beforecopy,false ); In Chrome / Safari, the above will run the "beforecopy" function when
document.observe('click', function(e, el) { if (e.target != Element.descendantOf('calendar')) { $('calendar').fade(); } }); I am
document.body.innerHTML works to search within the body. Is there a similar function for searching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.