0

Here's my jQuery function,

$.post("assets/scripts/chat/get_user_info.php", {UserID: UserID}, function(data){
if (data == "error") {
alertBar('negative','There was an error sending the message');
}
var username = data;
})

But I only use the "data" or "username" variable in the function itself, so how can I use it globally around the whole page?

2
  • create global variable username and assign the data in function. Commented Jan 20, 2012 at 17:27
  • Whatever answer you pick, do not forget to declare variables. It's perfectly legal to set a property in the global scope using window.username = data;. However, not declaring a variable using var is a bad practice, and will throw errors in strict mode. Commented Jan 20, 2012 at 17:29

8 Answers 8

5

Define variable in the global scope, outside your function.

var username, data;

$.post("assets/scripts . . .

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

Comments

2

Either

window.data = data

or

username = data

The thing is though,the $.post is asynchronous, so you won't be able to do something like...

$.post( ... )
alert(window.data);

Comments

1

Try

var globalData;
var username;

$.post("assets/scripts/chat/get_user_info.php", {UserID: UserID}, function(data){
    if (data == "error") {
        alertBar('negative','There was an error sending the message');
    }
    globalData = data
    username = data;
})

Comments

1

What you can do is

  var myglobaldata = {};

$.post("assets/scripts/chat/get_user_info.php", {UserID: UserID}, function(data){
if (data == "error") {
alertBar('negative','There was an error sending the message');
}
  myglobaldata.username = data;
})

Now you can access myglobaldata.username from anywhere.

Comments

0

You could save it as data using .data():

$.post("assets/scripts/chat/get_user_info.php", {UserID: UserID}, function(data){
    if (data == "error") {
        alertBar('negative','There was an error sending the message');
    } else {
        $(document).data('userInfo', data);
    }
});

Then you can access it by doing:

var userInfo = $(document).data('userInfo');

Comments

0
window.username = data

Will make it a global variable. Though this is often considered bad practice.

Comments

0

I assume your jQuery is wrapped in $(document).ready(); - try declaring the vars outside like this, which will make them global throughout the page:

var username, data;

$(document).ready(function() {
    $.post("assets/scripts/chat/get_user_info.php", {UserID: UserID}, function(data){
    if (data == "error") {
        alertBar('negative','There was an error sending the message');
    }
    username = data;
    })
});

1 Comment

The whole thing is inside a function.
0

Try document.username instead of username

document object is visible from everywhere

Comments

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.