Search code examples
javascriptjqueryvariablesparameter-passingawesomium

JQuery/JS: Retrieving parameter value from function


Say I have a function in JS function playerJob(job){}

I call this method from C++ using Awesomium, basically saying

int currentJob = GameModeState::changeJob(false);
mScreen->executeJavascript("map.playerJob(currentJob);");

All that I want you to see is that I give my JS function playerJob a value for it's parameter.


What I want to do is then use playerJob within my JS to find out what value job is. I want to set a variable to that value. I don't know how to get that value out of my method without having to enter a parameter. Say I have my method be

function playerJob(job) {
    return job;
}

And I try to get job by saying:

var currentJob = playerJob();

This does not work, probably because I need to put something within the parenthesis.

I know I could use playerJob like this:

function playerJob(job) {
    someOtherFunction(job);
}

But the problem is that I need this job variable before I call any regular "functions". I need to use it within a

$(document).ready(function()
{
   $("#div").click(function(e)
   {

Which I cannot really "call".

Is there a way using JQuery to get that variable from my playerJob function?


Solution

  • Maybe keeping a global variable is the correct solutution in this case.

    var currentJob=null;
    //var currentJob="Default Job;
    
    function playerJob(job) {
        if (job) currentJob=job;  //    <------- UPDATED
        someOtherFunction(job);
    }
    
    $("#div").click(function(e){
       if (currentJob) alert(currentJob);
    }