Access javascript functions defined in different files

I have two seemingly related issues related to the javascript function defined in different places. The first problem I ran into was calling a function that I defined from firgbug or safari console. I defined a function called getRed that looks like this:

function getRed(row, col) { // do something stuff and return the red value as a float } 

I would like to be able to test this function from the console, but every time I try to call getRed (1,1); for example, I get this error: ReferenceError: getRed not defined

Do I need to make a special call to determine the namespace? I define this function in a javascript file called drawing.js, which is defined very early on my html page.

Another problem I ran into was calling the function defined in the same drawing.js file from the onChange method: of my dojo color palette. Here is the code for the color palette:

 <script type="text/javascript" src="drawing.js"></script> //the method colorChange is inside drawing.js which is defined before the dojo //color palette <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js" djConfig="parseOnLoad: true"> </script> <script type="text/javascript"> dojo.require("dojox.widget.ColorPicker"); dojo.addOnLoad(function() { var c = new dojox.widget.ColorPicker({ onChange: function(val) { console.log("BEFORE"); colorChange(val); console.log("AFTER"); } }, "picker1"); }); </script> 

Here is the definition of changeColor inside the drawing.js file:

  function colorChange(val) { console("colorChange!"); } 

Each time I click on the color picker, I get the following error: ReferenceError: colorChange is not defined.

I am very new to javascript, and I am sure that these two problems have very similar and simple solutions, but I could not find the answer on the Internet. Can someone help me?

I am sure the script is loading, as this screenshot shows: enter image description here

+4
source share
2 answers

The console is in the same global area as your page. Since getRed() and colorChange() both defined in the .js drawing, and none of them can be found in the global scope, I suspect that drawing.js is not turning on properly.

To check if drawing.js is really enabled (i.e. you have the correct path to the file), go to the Firebug Script tab. It will display all scripts included on the current page.

+3
source

There is no need for additional configuration, all you need to do is make sure your draw.js is turned on, check the path to the file. If this is correct, there should be no problem with the call.

Just check if your js location is real, if your js file is in the root, just add a slash in front of the location so that it always downloads it from www.example.com/drawing.js. Add the language = "javascript" attribute to your <script ....

Hope this helps.

0
source

Source: https://habr.com/ru/post/1346622/


All Articles