It is possible to track the . js files that are called when running a Jquery event

Asked

Viewed 762 times

2

For example, if an event click applied in a link call a function that is in the file A.js, which in turn needs the B.js, who needs the C.js. I wonder if you have how to know this dependency between the files by browser.

2 answers

2

You can enter the firebug and put a break point in the body of the function. With this you can go running line-by-line and check in depth the other functions that are called.

  • 1

    And if you use Chrome or IE, they also include debuggers with the same functionality. I just don’t know about Opera.

  • Can I spice up the question a little? Not that I’m interested in this subject, but anyway... And to implement something similar but at a lower level so that it does not depend on the browser?

  • 1

    @Brunoaugusto No, you couldn’t intercept the language interpreter to treat breakpoints.

  • Cool, but already enriched the content of the topic anyway ;)

1

Except by debugging line-by-line, there is no way to predict the dependencies of a given function or chunk of code because they are solved dynamically.

If there is no eval involved, it would be possible, for example, to analyze the code statically and determine what functions are called. So you can search the entire code and see where they are.

However, note that this method is very flawed. There are numerous situations where it just doesn’t work.

You see, Javascript is not a strongly typed language, so we could do this:

var obj1 = {
    f: function() { return 1 }
};
var obj2 = {
    f: function() { return 2 }
};

function generica(obj) {
  return obj.f();
}

console.log( generica(obj1) );
console.log( generica(obj2) );
console.log( generica({ f: function() {return '?'} }) );

In the example, I created two objects obj1 and obj2, each with the function f(). Then I declared a function generica() which receives any object and performs the function f().

On the third call to generica(), create an anonymous object with another function f().

The console output will be:

1

2

?

Note that there is no way to know which function f()it refers, because it may not be one of the first two. We can only know at the moment when generica() is running.

In addition, in Javascript it is also possible to load and create code dynamically, so there is code that is simply not in any file.

In theory, the browser could scan all the code loaded in memory in a context setting, with the page loaded, in order to identify the possibilities. However, the usefulness of this would be doubtful.

I believe that this kind of difficulty causes that there are not many solutions (if any) to this tracking problem.

Welcome to the world of dynamic languages. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.