简体   繁体   English

通过外部页面和history.pushState一起进行的Ajax请求

[英]Ajax Request by external page and history.pushState together

There is a way for combine these two technologies so that they work together when we are alredy in the div "result" ? 有一种方法可以将这两种技术结合起来,以便当我们对div“结果”感到怀疑时它们可以一起工作?

Let's see the problem.. We have the first code that do the ajax request 让我们看一下问题。.我们有第一个执行ajax请求的代码

    var http_request = false;
function makeRequest(url,getvar,funzione) {
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            //http_request.overrideMimeType('text/xml');
            // See note below about this line
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Errore :( Non riesco a creare unna connessione XMLHTTP');
        return false;
    }
    http_request.onreadystatechange = funzione;
    http_request.open('POST', url, true);
http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_request.send(getvar);

}

function alertContents() {

    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            //alert(http_request.responseText);
    document.getElementById("result").innerHTML = http_request.responseText;
        } else {
            alert('C\'è stato un problema alla connessione.');

        }
    }else{
    document.getElementById("result").innerHTML ="loading";
}

}

And another function that edit the andress bar.. 还有另一个编辑Andress栏的功能。

jQuery(document).ready(function() {
   $('a.clickurl').click(function(event) {
      var currentPage = document.location.pathname.substring(document.location.pathname.lastIndexOf('/') + 1);
      if ($(this).attr('href') != currentPage){ 
         if (history && history.pushState) {  
            history.pushState(null, document.title, $(this).attr('href'));
               $.get($(this).attr('href'), {ajax:'1'}, function(data, text, xhr) {
                  pageSlider(data, text, xhr);
               });

            event.preventDefault(); 
         }
     }
});

after the first istance we got result on the div.. and up to here everything is working correctly, but how let the function work also in the links inside the "result" div? 在第一个距离之后,我们在div上获得了结果。到这里为止,一切都正常工作,但是如何让该函数在“结果” div内的链接中也起作用?

setting a href="#" the ajaxrequest work correctly and just reflash the "result" div.. but if i set an different address loads the entire page.. 设置href =“#”,ajaxrequest可以正常工作,只是刷新“ result” div ..但是,如果我设置了其他地址,则会加载整个页面。

ps. ps。 i already tried return false; 我已经尝试过返回false;

Problem is the fact that you are not binidng events to the dynamic content. 问题是您没有将事件绑定到动态内容。 You need to either rebind or use event delegation. 您需要重新绑定或使用事件委托。

$(document).on("click", 'a.clickurl', function(event) { 

or even better if all the links are only in the result div 甚至更好,如果所有链接都只在结果div中

$("#result").on("click", 'a.clickurl', function(event) { 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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