Jayesh
Jayesh

Reputation: 3951

Adding data to an existing function dynamically

I was wondering if we can add data to a function which is already defined in Javascript.

    var win;
    window.onload= function()
    {

        win = window.onbeforeunload;
        win = win.toString();

        var firstLetter = win.indexOf('{');


        win = win.substring(firstLetter + 1, win.length - 1);
        //Getting the data of the function [firstLetter = first occurrence of { ]

        win = win + 'alert("More Unload");';

        `/* Here, I want to rebind var win to the window.onbeforeunload event*/`
    }   

    window.onbeforeunload = function () 
    {
        alert("Unload function");
        alert("More Unload");
    }

Is this possible?

Thanks.

Upvotes: 0

Views: 93

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318748

You can create a function using new Function(yourCodeString).

However, this kind of code rewriting is extremely messy and if you need it you are doing something wrong for sure!

Upvotes: 1

Related Questions