0

I have the following function that works in Chrome/IE:

for(var i = 0; i < 5; ++i) {
        document.all["elems" + i].onscroll = getFunc("onScrollAdj(document.all['tb" + i + "']);");
    }

function getFunc(jsString) {
    return new Function(jsString);
}

However I get the error:

ReferenceError: event is undefined.

I have tried to re-write the function to include an event however I then get another error:

var i is undefined.

document.all["elems" + i].onscroll = onScrollAdj(event, document.all['tb" + i + "');

Is there any way to ensure both event and attributes can be passed?

1
  • what is onScrollAdj - I bet that has a reference to event - which used to be an internet explorer "kludge" that for some reason Chrome decided to implement Commented Sep 21, 2016 at 11:09

1 Answer 1

2

however I get 'ReferenceError: event' is undefined.

That's because you're trying to use event without declaring it as an argument. That only works on Microsoft browsers, which make event a global, and Chrome which throws a bone to Microsoft-only code. You will need to declare the argument.

There's no need for new Function virtually ever, and certainly not in this case:

for(var i = 0; i < 5; ++i) {
    document.all["elems" + i].onscroll = getFunc(i);
    // ------------------------------------------^
}

function getFunc(i) {
// --------------^
    return function(event) { onScrollAdj(event, document.all['tb' + i]); };
    // -------------^--------------------^
}

Note that you'll have to ensure that onScrollAdj accepts event:

function onScrollAdj(event, tbThingy) {
// ------------------^
Sign up to request clarification or add additional context in comments.

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.