Search code examples
javascriptproj4js

Javascript question on functions


In my main Web Page (Viewer.aspx) I have a javascript script tag like this

<script language="javascript" type="text/javascript">

function initialize() {
     var map = $find('Map1');           
     map.add_mouseMove(mouseMove);  

 }

</script>

Within those script tags I have a function. Is it possible to call another function that is in a different script tag like this?

<script language="javascript" type="text/javascript" src="Resources/JavaScript/proj4js-combined.js">

function mouseMove(sender,eventArgs) {
     var source = new Proj4js.Proj('EPSG:3116');
        var dest = new Proj4js.Proj('WGS84');

        var p = new Proj4js.Point(px, py);
        Proj4js.transform(source, dest, p);    
 }

</script>

Solution

  • As per your comment, here's what T.J. was talking about - you need to turn your second script block into something like this:

    <script type="application/javascript" src="Resources/JavaScript/proj4js-combined.js"></script>
    <script type="application/javascript">
    
    function mouseMove(sender,eventArgs) {
         var source = new Proj4js.Proj('EPSG:3116');
            var dest = new Proj4js.Proj('WGS84');
    
            var p = new Proj4js.Point(px, py);
            Proj4js.transform(source, dest, p);    
     }
    
    </script>
    

    ...but you should really just move the inline code block (what's inside of the 2nd <script> tag in my answer) to an external Javascript file.


    Edit 1: What's the programming background you're coming from? If it's something like C# or Java, you'll need to forget what you know about those and approach Javascript completely differently. Javascript is an interpreted language, not a compiled one; among other things, this means that the order in which your functions are declared matters.

    When I say "function declaration," I mean anything that looks like this:

    function myNewFunction()
    {
       // anything else here
    }
    

    This tells the Javascript interpreter about a new function, called myNewFunction, whose body consists of whatever is in the curly braces.

    Here's an example of what I'm talking about when I say you are using a function before you've declared it. Consider the following block of code (in isolation from any other Javascript, say, in an external Javascript file):

    function foo() // this line declares the function called "foo"
    {
    
    }
    
    function bar() // this line declares the function called "bar"
    {
        foo(); // this line invokes the previously declared function called "foo"
    }
    

    This will work as expected, because foo() was declared before you called it. However, what (it sounds like) you're trying to do is analogous to this:

    function foo() // this line declares the function called "foo"
    {
        bar(); // this line tries to invoke the function called "bar"
               // but it hasn't been declared yet! so it's undefined
    }
    
    function bar() // this line declares the function called "bar"
    {
    
    }
    

    If you were to run this second Javascript snippet, it wouldn't work, because you're calling a function before it was declared.*

    *Footnote: this is not actually the case, because using this syntax (function foo() { ... }) does something special and magical in Javascript. My particular example will actually work, but it illustrates the problem you're having.