简体   繁体   中英

jquery function called twice

i have working on ajax search functionality but facing one issue.let me quick explain sample code below.

function search_content(sh_val,sh_act)
{
    ....some search code.....
    window.location.hash = sh_val+'@'+sh_act;

}

I have use following function for restore the search when clicked on browser back or forward button.

$(window).bind( 'hashchange', function(e) {
var pagee=location.hash.slice(1)
if (pagee!="")
    {
        search_content(pagee,'catsh');
    }
    return false;
 });

but i am facing issue when i am search ajax search function is called twice. $(window).bind( 'hashchange') is also called when i am searching.is there any way to call the above function only for browser back and forward button or any other solution ?

how can i resolve this issue?

I would appreciate anykind of help..

try .on() instead of bind and look for manual triggers. Also a stop propagation maybe? Why applying on window instead of document?

You can also add a debugger command :

$(window).on( 'hashchange', function(e) {
    debugger;
    e.stopPropagation();
    [...]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM