1

I am using an inverse geolocation method from mapquest that looks something like this

function fieldVia_changed(a)
            {
                if (document.getElementsByName("via"+a)[0].value.trim().length!=0)
                {
                   var via = document.getElementsByName("via"+a)[0].value ;
                   var strV = via.replace(/ |,/g, "+");
                   var s = document.createElement('script');     
                   s.src = 'http://open.mapquestapi.com/nominatim/v1/search?q='+strV+'&json_callback=cbv&format=json&polygon=1&addressdetails=1';
                document.getElementsByTagName('head')[0].appendChild(s);
                }

            }

The results of the request are processed in the function cbv which accepts a parameter

 function cbv(json) 
            {
                v_lat[0] = json[0].lat;
                v_lng[0] = json[0].lon; 
             }

However i need to be able to pass another parameter to the cbv function from fieldVia_changed function so that i can process the information properly. The cbv function definition would look like this function cbv(json,a). I looked all over but i can not find a solution. Is it possible ?

2
  • What you're doing is really JSONP, and the function is called from the included script as a sort of callback for when the script has loaded, and you can't add additional arguments to that function call. Commented Aug 6, 2014 at 12:51
  • I'm not sure what you would need to pass back as a second argument. Couldn't you include it in the "json" argument? Commented Aug 6, 2014 at 12:52

1 Answer 1

1

The server side won't usually have the option of passing additional arguments in a JSONP system. A possible solution is to use the value of a in the callback function name, and dynamically create the function as a kind of man in the middle between the cbv() function, allowing you to pass a as a second argument.

function fieldVia_changed(a) {
    if (document.getElementsByName("via" + a)[0].value.trim().length != 0) {

        // dynamically create the function
        window['cbv_' + a] = function (json) {
            cbv(json, a);
        };

        var via = document.getElementsByName("via" + a)[0].value;
        var strV = via.replace(/ |,/g, "+");
        var s = document.createElement('script');
        // call the function cbv_x
        s.src = 'http://open.mapquestapi.com/nominatim/v1/search?q=' + strV + '&json_callback=cbv_' + a + '&format=json&polygon=1&addressdetails=1';
        document.getElementsByTagName('head')[0].appendChild(s);
    }
}

function cbv(json, a) {
    v_lat[0] = json[0].lat;
    v_lng[0] = json[0].lon;
    console.log(a);
}

This is OK if a is some sort of short identifier, if it's from user input then it's not really suitable for use in the function name. You're using in the name attributes so I'm assume this is fine.

Sign up to request clarification or add additional context in comments.

5 Comments

I have tried this method, but apparently it does not reach the dynamic created function, it goes straight to the cbv[number] function which is not defined .
What is the content of a? It can only be what's acceptable in a JS function name. Press F12 and look at the Net tab, what does the URL look like when it gets called?
a is just a number. The URL looks like this open.mapquestapi.com/nominatim/v1/…
That URL shows cbv3 so you must have deleted the underscore because in the answer I use cbv_' + a in the URL. If you don't want the underscore then remove it from the dynamic function as well on line 5.
Shoot i forgot the underscore. Thanks a lot. Its working now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.