Skip to main content
jeffery wright
Inspiring
November 16, 2018
Question

AnimateCC Canvas: Function is Undefined?

  • November 16, 2018
  • 1 reply
  • 2094 views

Really?

It's defined on the very first frame, see:

function url_PP (){

window.open(pp_URL, "Product_Tab");

}

btw, this produces the same error:

this.url_PP=function(){

window.open(pp_URL, "Product_Tab");

}

So, where is 'url_PP' is undefined coming from?

which is generated from this line on the fourth frame, separate layer:

this.b_PP.addEventListener("click", url_PP);

and the variable is declared a line before that:

pp_URL = "http://www.website.com";

So yes, it is defined, what gives?

Any idea?

Thanks!

    This topic has been closed for replies.

    1 reply

    Legend
    November 16, 2018

    Functions defined with just a function declaration only exist in the frame where they're defined. So of course you can't call it in another frame. It doesn't exist anymore.

    Functions defined as methods of the this object CAN be accessed anywhere in the same movieclip. But only if you remember to call them as a method of this, which you failed to do.

    The above also applies for variables. "var myURL" will only be accessible in its frame. "this.myURL" will be accessible to the entire clip.

    BTW, having a variable named pp_URL and a function named url_PP is an insanely bad idea, for reasons I hope would be self-evident.

    jeffery wright
    Inspiring
    November 16, 2018

    Thanks for the response, fwiw I changed the function name to open_URL, but I doubt that will change anything.

    function open_URL (){

    window.open(pp_URL, "Product_Tab");

    }

    I also tried

    this.b_PP.addEventListener("click", this.open_URL );

    Which instead of working, produced an even more enigmatic and useless error:

    Unable to get property 'handleEvent' of undefined or null reference in IE

    TypeError: h is undefined in FFox...

    All I want is for one button to open various url's defined in the variable pp_URL, open whatever the current value of pp_URL is declared to be.

    I placed the addEventListener in the same frame as the function and removed it's "this" and all seems to be working for now... what a crazy process just to get this to do something so simple.

    If for some reason I actually wanted a program that produces lots of mysterious and unhelpful error messages, this program would be my top choice.

    Thanks again!

    Legend
    November 16, 2018

    The errors are produced by the respective browsers' JavaScript engines, because your JavaScript code is broken. You don't seem to have paid any attention to what I just told you about how function scoping works, because you keep defining things in an inaccessible way.

    Look, this is very simple...

    Frame 1:

    var _this = this;

    this.open_URL = function () {

        window.open(_this.pp_URL, "Product_Tab");

    }

    The _this is necessary because (unless you use bind), event handlers call functions in the window scope instead of the local scope. pp_URL could have been placed in the global scope, on the window object, but that's considered bad practice (for good reason).

    Then on frame whatever:

    this.pp_URL = "https://www.website.com/";

    this.b_PP.addEventListener("click", this.open_URL);

    HOWEVER...

    You could save yourself all this trouble by just using the on method, which supports passing data to an event handler.

    Frame 1:

    this.open_URL = function (evt, url) {

        window.open(url, "Product_Tab");

    }

    Frame whatevs:

    this.b_PP.on("click", this.open_URL, this, false, "https://www.website.com/");