История.JS передает переменную в вышеуказанную функцию

#javascript #jquery

#javascript #jquery

Вопрос:

Я хочу передать переменную loadurl в указанную выше функцию. Как я могу этого добиться? (приведенный ниже код — это моя функция, и я хочу передать идентификатор приведенному выше коду. Любая помощь приветствуется.

$(function() {

 // Prepare
var History = window.History; // Note: We are using a capital H instead of a lower h
if ( !History.enabled ) {
     // History.js is disabled for this browser.
     // This is because we can optionally choose to support HTML4 browsers or not.
    return false;
}

// Bind to StateChange Event
History.Adapter.bind(window,'statechange',function() { // Note: We are using statechange instead of popstate
    var State = History.getState();



    console.log(loadurl);
    $('#test').load(loadurl);
    /* Instead of the line above, you could run the code below if the url returns the whole page instead of just the content (assuming it has a `#content`):
    $.get(State.url, function(response) {
        $('#content').html($(response).find('#content').html()); });
    */
    });


// Capture all the links to push their url to the history stack and trigger the StateChange Event
$('a').click(function(evt) {
    evt.preventDefault();
    var loadurl = $(this).attr('id');



    History.pushState(loadurl, $(this).text(), $(this).attr('onclick'));
});
  

});

Ответ №1:

Вы можете создать глобальную переменную в верхней части страницы, присвоить ей значение в одной из ваших функций, а затем использовать ее в другой функции.