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 !

Jon Miles
  • 9,605
  • 11
  • 46
  • 66
Clément Andraud
  • 9,103
  • 25
  • 80
  • 158
  • 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 '14 at 13:22
  • Just declare variable globally and use it..why are you messing up with window object? – Deepak Ingole Jan 24 '14 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 '14 at 13:23
  • I have the variable just_id in my first function, i can't retrieve this id in other... – Clément Andraud Jan 24 '14 at 13:23
  • http://stackoverflow.com/questions/3352020/jquery-the-best-way-to-set-a-global-variable – Somnath Kharat Jan 24 '14 at 13:25
  • @Pilot using `window.foo = 'bar'` is actually safer (and also permitted in strict mode) than just saying `foo = 'bar'` without a `var` qualifier. It makes it clear that the explicit intent is to declare a global (however bad an idea that may be). – Alnitak Jan 24 '14 at 13:27
  • Why are you performing an ajax call if you aren't using any of the fetched data in the `success` fuction? – plalx Jan 24 '14 at 13:34

3 Answers3

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');
    }
});
Vin.AI
  • 2,369
  • 2
  • 19
  • 40
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');
    }
});
Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
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

Dmytro Filipenko
  • 861
  • 1
  • 9
  • 25