views:

66

answers:

3

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>
+5  A: 

Yes, this is done quite regularly as Javascript functions can be put into other files and pulled into pages that work this way.

JB King
+4  A: 

Your second script tag specifies a src. The contents of the .js file will be loaded and parsed, but the code inside the script tag (the mouseMove function) will be ignored. If you want both the function and the contents of the .js file, you need to separate them into two different script tags.

jhurshman
+1  A: 

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.

Matt Ball
That makes sense to me now, thanks. I'm still getting mouseMove is undefined so do I need to do anything extra at map.add_mouseMove(mouseMove) to get it to jump to that function??
Josh
It sounds like you're calling `map.add_mouseMove(mouseMove)` before you've defined your `mouseMove` function. Where is the code that executes `map.add_mouseMove(mouseMove)`?
Matt Ball
In my initial post that is pretty much how I have it. I incorporated the additional code you and others suggested in second script tag but that's about it
Josh
That can't be right, because your question shows you're still combining inline and external script tags. But, it looks like I'm right, that you're calling `map.add_mouseMove` before you've defined your `mouseMove` function.
Matt Ball
in the first script tag above the function called initialze I have this code Sys.Application.add_init(initialize);I don't really understand what you mean by "defined the mouseMove function". I can't simply call that function even though it's in another script tag? Sorry, I'm still learning
Josh
I'm making an edit to my answer because this might get a bit long-winded.
Matt Ball
map.add_mouseMove is working fine. It's the function that it needs as a parameter that its having trouble finding. I changed the code to map.add_mouseMove(mouseMove1); and then changed the function in the second script tag to mouseMove1 and I still get a mouseMove1 is undefined. Does that help any? I'm probably leaving something out
Josh
Could you update your question so that it accurately reflects your code, and the problem? Just insert your new content below what you've already got.
Matt Ball
Thanks for all your help Bears. It has helped me for the original purpose of my question. I will start another post then
Josh
No problemo. See you after the jump
Matt Ball
You have to distinguish between parsing and execution time. In your second foo/bar example, both foo and bar are parsed before foo is executed. For this reason, everything works fine and it is irrelevant in which order the two functions were declared. Furthermore, within a single `script` tag, all function declarations are parsed before any non-function declarations. So even this works: `<script type='text/javascript'>test(); function test(){alert('test')}</script>`. However, if the function was declared in a later `script` tag, it won't be available until that tag is parsed.
jhurshman
@jhurshman - I know the difference, as per my footnote. I was using it for illustration purposes.
Matt Ball