-2

So I have two javascript files, mapper.js has a function named goMap(map_num), the structure of goMap() is

  function goMap(map_num){
      var som_var;
      switch(map_num){
         case 0: /*do stuff to "some_var"*/ return some_var; break;
         case 1: /*do stuff to "some_var"*/ return some_var; break;
         case 2: /*do stuff to "some_var"*/ return some_var; break;
         case 3: /*do stuff to "some_var"*/ return some_var; break;
      }

  }

In my other javascript file I call goMap() with

  var params;
  $.getScript(mapper_script, function(){  
                     params =  goMap(map_num); 
                     /*Do Stuff that relies on params*/
           });

but, when I check the variable params, nothing is in there. I did make sure that the function was being executed, so that is not an issue. Anyone got any idea why this is happening?

Edit: This Works, the problem was an issue with multidimensional arrays.

user962028
  • 59
  • 1
  • 7
  • 4
    code tip: you don't need for `break` after a `return`, because you're cutting off execution already. That said, if you keep the `break` statements, you can move the `return some_var` to after your switch block, and only have it once. – Mike 'Pomax' Kamermans Jun 09 '13 at 22:02
  • What is `map_num` and where does it come from? Are you waiting for `$.get` to return before checking `params`? – mu is too short Jun 09 '13 at 22:03
  • @mike cool, thx for the tip, I read somewhere that you need to do this in php, so that is what I was used to. – user962028 Jun 09 '13 at 22:04
  • you don't need it in PHP, either. PHP will cut execution on a return too =) – Mike 'Pomax' Kamermans Jun 09 '13 at 22:06
  • @mu map_num is a number so 0,1,2, etc. it is a variable in the function I am executing the $.getScript, and yes I made sure to $.get returned before checking params – user962028 Jun 09 '13 at 22:06
  • @mike http://stackoverflow.com/questions/1437461/php-coding-styles-return-in-switch-case <- blame that guy – user962028 Jun 09 '13 at 22:07
  • But what is `map_num` when you `goMap(map_num)`? – mu is too short Jun 09 '13 at 22:12
  • And where are you checking `params`? How do you know that the `$.get` AJAX call has gotten a successful response from the server? – mu is too short Jun 09 '13 at 22:14
  • possible duplicate of [How to return AJAX response Text?](http://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text) – Quentin Jun 09 '13 at 22:19
  • 1
    "Edit: This Works, the problem was an issue with multidimensional arrays." Then why did you accept a completely unrelated answer? – Matt Ball Jun 10 '13 at 04:15
  • @matt Long story short, I was debating to use $.get or $.getScript, I originally tried both before asking the question, but I was 80% sure it was $.getScript. I left $.get in my code by accident and copy pasted over to the question, which is y you see mu talk about $.get, I accepted Mikes answer because he was right, it was $.getScript, but I had another completely unrelated issue that was screwing up my codes logic, which was the multi array. – user962028 Jun 10 '13 at 05:34

1 Answers1

2

I assume this is jQuery's normal $.get() function, in which case you've simply retrieved the data that you get when you load the script's URL, as plain text. You probably meant to use $.getScript() instead, which loads JavaScript from file and makes sure it gets run.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153