History.JS将var传递给上面的函数

时间:2016-10-04 19:38:38

标签: javascript jquery

我想将var loadurl传递给上面的函数。我怎样才能实现这一目标? (下面的代码是我的函数,我想将id传递给上面的代码。感谢任何帮助。

$(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 个答案:

答案 0 :(得分:1)

您可以在页面顶部创建一个全局变量,在其中一个函数中为其赋值,然后在另一个函数中使用它。