Sakshi Sharma
Sakshi Sharma

Reputation: 1472

make value of a function global to be called in any other function

hello i want to pass the value of a function to any other function or make the value of a function global so that it can be called in any other function . but i dnt knw how to do it , tried some codes still no sucess .

here is the function code

function newalbum(val1,loginuser)
{

$.ajax({
type:"GET",
url: 'ajax/album.php?val='+val1+'&& loguser='+loginuser,
success: function(data) {
$("#newalbum1").html(data);
        var ab=$('#ab').val();
        var abc=$('#loginuser').val();
        var action=$('#action').val();

alert(ab);
}
});
}

in this function i want to make the three variables var ab, var abc, var action to be global so that its value can be used in any other function . can any one help?

edit - the value of var ab, var abc, and var action is comming from ajax data that is why i am not able to use those variable directly , it says undefined if i use it directly.

after the varibales value received from ajax in function newalbum . i want to use those varibales inside

 (function( $ ){
 $.fn.Uploadrr = function( options ) {

Upvotes: 2

Views: 126

Answers (3)

AndreM
AndreM

Reputation: 66

When you call "var" before starting a new var it'll address a new place in memory. So just start the vars outside the functions and access them inside like this:

var ab, abc, action;

function newalbum(val1,loginuser)
{

$.ajax({
type:"GET",
url: 'ajax/album.php?val='+val1+'&& loguser='+loginuser,
success: function(data) {
$("#newalbum1").html(data);
        ab=$('#ab').val();
        abc=$('#loginuser').val();
        action=$('#action').val();

alert(ab);
}
});
}

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69905

Instead of using those variables by making them global why don't you use directly $('#ab').val(), $('#loginuser').val() and ('#action').val()?

Upvotes: 0

Rob W
Rob W

Reputation: 349012

You can declare the variables in the global scope, and remove var before the ab, abc and action variables. Another method is to prefix the variables with window, the global object.

Method 1:

var ab, abc, action; // In the global scope
...
    ab = ...;        // Inside the function

Method 2:

window.ab = $('#ab').val(); // Inside the function

Note: Unless you've got an extremely good reason for it, you usually should not declare global variables.

Upvotes: 4

Related Questions