简体   繁体   English

构建一个Ajax系统以通过链接类加载新页面内容

[英]Building an Ajax System for loading New Page Content via link classes

The idea is each page has the same header and footer exactly, but each page has inner content that will change each page load 这个想法是每个页面具有完全相同的页眉和页脚,但是每个页面都有内部内容,这将改变每个页面的负载

<header>blah blah blah</header>
<div id="main-content">Changes Each Page, but with links like <a href="anotherpage.html" class="ajaxify">Go to Another Page with smooth animation</a></div>
<footer>blah blah blah</footer>

I have written this script based off an earlier version I saw somewhere, however I am having an issue where no scripts are being fired. 我已经根据在某处看到的较早版本编写了此脚本,但是我遇到了一个问题,即没有脚本被解雇。 The site's overall scripts are all loaded on the initial page load, and I just need the scripts to be re-run each time you click on a link, that way I can have the seamless effect of the site pages loading inside the page, and adding custom CSS animations when each custom page loads. 网站的整体脚本都在初始页面加载时加载,并且我只需要在每次单击链接时都重新运行脚本,这样我就可以无缝加载网站页面到页面内部,并且在每个自定义页面加载时添加自定义CSS动画。

var newHash     = '',
   $mainContent = $('#main-content');

  $(".ajaxify").click(function(event) {
  event.preventDefault();
  newHash = $(this).attr('href');
  history.pushState(null, null, newHash);      
  $mainContent.load(newHash + " #main-content > *");
  return false;
});

I attempted to add a .getscript block when ajax is completed, but I found that it starts making the page loads really heavy and starts firing off errors. 我尝试在ajax完成时添加.getscript块,但是我发现它开始使页面加载变得非常繁重,并开始触发错误。

$(document).on("ajaxComplete",function(){
    $.getScript( "http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" );
    $.getScript( "http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js" );
    $.getScript( "../js/libraries.js" );
    $.getScript( "../js/footer.js" );
});

What am I missing? 我想念什么?

Big thanks to beercohol, he set me on the right direction. 非常感谢啤酒酒精,他使我朝着正确的方向前进。 I didn't need to reload the whole javascript stack from the head each time; 我不需要每次都从头重新加载整个javascript堆栈; just the javascript in the footer. 只是页脚中的JavaScript。 This solved so many issues at once. 这一次解决了很多问题。

EDIT: I also discovered that by changing it to "delegate" vs onclick, I dont' need to reload the initial ajax script again. 编辑:我还发现,通过将其更改为“委托” vs onclick,我不需要再次重新加载初始ajax脚本。 Very handy and keeps the pages light. 非常方便,使页面保持明亮。

Final Code example is this: 最终代码示例如下:

   var newHash     = '',
   $mainContent = $('#main-content');

  $('body').delegate(".ajaxify", "click", function() {
  event.preventDefault();
  $("html, body").animate({ scrollTop: 0 }, 500);
  newHash = $(this).attr('href');
  history.pushState(null, null, newHash);
  $mainContent.delay(500).load(newHash + " #main-content > *");
  return false;
});
$(document).on("ajaxComplete",function(){
   jQuery.get('/js/footer.js', function(data) { eval(data); })
});

Assuming your scripts are loaded in your header, then you don't need to reload them with .getScript . 假设脚本已加载到标头中,则无需使用.getScript重新加载它们。 That would just keep adding them into the page again for every link you click! 只要将您单击的每个链接重新添加到页面中,就可以了!

Your main function looks mostly correct - although it seems to be missing the first line - but the .load doesn't look quite right. 您的主要功能看起来大部分正确-尽管似乎缺少第一行-但.load看起来不太正确。 Try something like this: 尝试这样的事情:

$(document).ready(function() {
    var newHash     = '',
    $mainContent = $('#main-content');

    $(".ajaxify").click(function(event) {
    event.preventDefault();
    newHash = $(this).attr('href');
    history.pushState(null, null, newHash);      
    $mainContent.load(newHash + ' #main-content');
    return false;
});

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

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