Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access variable inside jquery from regular javascript function

Tags:

javascript

I am a newbie to the jquery. I am trying to access a variable defined inside jquery block outside the jquery (from regular function) like this, but I can't access it. Could somebody tell me how?

<script language="javascript">
$(function()
{
    .......
    .......
    .......
    var api = pane.data('jsp');
    var current_location = api.getContentPositionX();
}

function change_title(t_index) {
    alert("print="+$.current_location);
    window.location.href="page.php?p="+$.current_location);
}

I want to get a value for $.current_location.

Thanks.

like image 499
user826323 Avatar asked Jul 08 '11 06:07

user826323


1 Answers

There is no such thing as a "jQuery variable", they are all regular Javascript varaibles.

The reason that you can't access the current_location variable from your function is that the variable is declared locally inside another function.

Just declare the variable outside the functions so that it's global, and you can access it from both functions:

var current_location;

$(function() {
    .......
    .......
    .......
    var api = pane.data('jsp');
    current_location = api.getContentPositionX();
}

function change_title(t_index) {
    alert("print=" + current_location);
    window.location.href = "page.php?p=" + current_location;
}
like image 91
Guffa Avatar answered Nov 15 '22 11:11

Guffa