0

So, i'm in an ajax request, in the success callback :

var just_id = $(this).attr('id');
$.ajax({
                type: "GET",
                url: "/tw/www/search/",
                data: {search:tw_search,  type:'tw_search'},
                success: function (html) {
                            window[just_id] = $(this).attr('tw_username');
                }
            });

With this code, after i call with ajax my script, i need to create a variable with the name of a specific element.

So, if i'm on a <div id="test"></div>, in var just_id i have test and then, i create a variable test with window[just_id].

But i need to retrieve this variable in an other function on my page.. How can i do that ? I need to create a global variable with windows[]... Thanks !

7
  • 1
    You can retrieve it in the same way. window['test']. Or do you need the 'test' bit in a global variable?
    – putvande
    Jan 24, 2014 at 13:22
  • Just declare variable globally and use it..why are you messing up with window object? Jan 24, 2014 at 13:22
  • 1
    Unless you're creating a global name space, you don't "need to create a global variable". There is always a better solution.
    – Alnitak
    Jan 24, 2014 at 13:23
  • I have the variable just_id in my first function, i can't retrieve this id in other... Jan 24, 2014 at 13:23

3 Answers 3

0

Use window to declare global variable:

window.globalVar[];// i have not give window as varible name this may cause error it must be reserved
var just_id = $(this).attr('id');
$.ajax({
    type: "GET",
    url: "/tw/www/search/",
    data: {search:tw_search,  type:'tw_search'},
    success: function (html) {
        globalVar[just_id] = $(this).attr('tw_username');
    }
});
0
0

NB! First of all, if you are using this in success callback in ajax, this would have different context, not what you are expecting.

Answer: you can define some variable in top of function declaration

var just_id = $(this).attr('id'),
    attrUsername;

$.ajax({
    type: "GET",
    url: "/tw/www/search/",
    data: {
        search: tw_search,
        type: 'tw_search'
    },
    success: function (html) {
        attrUsername = $(this).attr('tw_username');
    }
});

Then, you can access to this variable. Avoid to use global variable for your purpose

0

Can't directly assign indexed value to window object. But you can do in this way:

window.x = new Array();
window.x[4] = 'value here';

// And can read from any function like here
;(function(){
 alert(window.x[4]);
})();

For your script above:

window.global = new Array();
window.global[just_id] = $(this).attr('id');
$.ajax({
    type: "GET",
    url: "/tw/www/search/",
    data: {search:tw_search,  type:'tw_search'},
    success: function (html) {
       window.global[just_id] = $(this).attr('tw_username');
    }
});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.