AJax技术

如何正确地实现一个自定义的AJAX(2)

字号+ 作者:H5之家 来源:H5之家 2017-04-10 18:02 我要评论( )

About the behaviour:Rev slider parallax content, will become scrambled from the second link visit (Home or Parallax pages); The Visual Composer parallax content (the guy at the desk picture, About us

About the behaviour: Rev slider parallax content, will become scrambled from the second link visit (Home or Parallax pages); The Visual Composer parallax content (the guy at the desk picture, About us page for example) is fixed on the first link visit - after F5 will be fine;

The menu mynewmenu.js will remember the state on session, so u'll have to close the browser in oder to visit multiple direct links properly.

I've received an answer from Rev slider support team telling me:

The best option for Ajax is to just add the slider's shortcode to a regular page/post, and then the slider's "init" script is will automatically be included with the slider's HTML. Then when the slider's HTML is removed from the DOM, all jQuery events are also removed. So all you really need to do is pull in the slider as page/post content, and then you won't need any custom script for the slider specifically.

Unfortunately I have no idea how can I approach this, implementing the above sugestion into my already received solution.

Could be something related to API(?) I've found these infos on Revolution slider / Visual Composer pages. Any thoughts?

解决方案

It looks like you are using a WordPress Theme so before you go messing around with the codes you need to know how changes are going to effect the website.

The way I build a PHP website is my header is contained in a PHP file and is retrieved using

<?php include('include/header.php'); ?>

You are using (PHP) server side scripting to retrieve the information from a database using client side scripting, at least that is how it appears. A possible solution may be to rewrite your header and move the java script to an external file

本文地址:IT屋 » How to properly implement a custom ajax

问 题

为了保持logotext &LT; DIV CLASS =“小7中4列标志”&GT; 键,菜单&LT;导航类=“pagedMenu”角色=“导航”&GT; ,不削波刷新页面或在内容从一个页面到另一个加载,我试图实施该解决方案通过的 @Buzinas (特别感谢)。在几句话:

在的header.php 我们有这个脚本:

&LT; HEAD&GT; ... &LT;脚本&GT; 功能AJAX(URL,回调方法,则params){ 如果方法=“GET”(方法!); VAR XHR =新XMLHtt prequest(); xhr.open(方法,URL); 如果(回调)xhr.addEventListener('负荷',函数(){ callback.call(这一点,XHR); }); 如果(PARAMS){ PARAMS = Object.keys(PARAMS).MAP(功能(键){ 返回EN codeURIComponent(键)+“=”+ EN codeURIComponent(PARAMS [关键]); })加入('和;'); xhr.send(PARAMS); } 其他 { xhr.send(); } } //自定义Ajax内容加载函数 功能ajaxRevslider(OBJ){ // obj.type:帖子类型 // obj.id:内容ID可加载 // obj.aspectratio:集装箱/媒体的宽高比 // obj.selector:容器选择,其中阿贾克斯的内容将被注入。它是通过必要的网格上返回的内容进行 VAR内容=“”; 数据= {}; data.action ='revslider_ajax_call_front'; data.client_action ='get_slider_html'; data.token ='&LT;?PHP的回声wp_create_nonce(“RevSlider_Front”); ?&GT;'; data.type = obj.type; data.id = obj.id; data.aspectratio = obj.aspectratio; //同步Ajax请求 jQuery.ajax({ 类型:“后”, 网址:“?&LT; PHP的回声ADMIN_URL(”管理-ajax.php');&gt;“中, 数据类型:“JSON”, 数据:数据, 异步:假的, 成功:函数(RET,textStatus,XMLHtt prequest){ 如果(ret.success ==真) 内容= ret.data; }, 错误:函数(E){ 执行console.log(E); } }); //先返回内容时加载它! 返回的内容; }; //自定义AJAX函数删除滑块 功能ajaxRemoveRevslider(OBJ){ 返回的jQuery(obj.selector +“.rev_slider”)revkill()。 } document.addEventListener(“DOMContentLoaded”,函数(){ VAR主要= document.querySelector('DIV [角色=主]“), 微调= document.querySelector('div.sk微调“), 页= {}; window.addEventListener('负荷',函数(){ toggleSpinner(假); }); 功能toggleSpinner(二){ spinner.classList [B? “删除”:“加”(“隐藏”); 的document.getElementById('包装')。style.opacity = B? 0:1; } 功能changePage(URL,标题){ 的setTimeout(函数(){ window.SITE.init(); window.vc_js(); },0); history.pushState({ HTML:main.innerHTML, 标题:标题 },'',网址); toggleSpinner(假); } 的document.getElementById('菜单菜单-2')。的addEventListener('点击',功能(五){ VAR EL = e.target; 如果(el.tagName ==='A'){ 即preventDefault(); toggleSpinner(真正的); 如果(页[el.href]){ main.innerHTML =''; main.appendChild(页[el.href]); changePage(el.href); } 其他 { 阿贾克斯(el.href,功能(XHR){ VAR FRAG = document.createRange()createContextualFragment(xhr.responseText)。 main.innerHTML ='&LT; DIV&GT;“ + frag.querySelector('DIV [角色=主]“)的innerHTML +'&LT; / DIV&GT;'; //pages[el.href] = main.firstElementChild; VAR _currentScripts = [] .slice.call(document.querySelectorAll('脚本')); [] .forEach.call(frag.querySelectorAll(“脚本”),功能(EL,I){ 如果((el.src ===''和;&安培; el.parentNode) || el.src.indexOf(“滑块”)&GT; = 0 || el.src.indexOf('滚动')&GT; = 0 || el.src.indexOf(“卖方”)&GT; = 0 || el.src.indexOf(“作曲家”)GT; = 0 ){ VAR S = _currentScripts.filter(函数(X){ 返回x.src === el.src; }); 而(s.length){ 如果(S [0] .parentNode) S [0] .parentNode.removeChild(S [0]); s.shift(); } document.body.appendChild(EL); } }); [] .forEach.call(frag.querySelectorAll(“风格”),功能(EL,I){ document.querySelector(“头”)的appendChild(EL)。 }); changePage(el.href,frag.querySelector('标题')的textContent。); }); } } }); window.addEventListener('popstate',功能(五){ 如果(e.state){ main.innerHTML = e.state.html; document.title时= e.state.title; } }); }); &LT; / SCRIPT&GT; ... &LT; /头&GT;

下面的 jQuery的-ready.js 注册/入队中的脚本calls.php :

(功能($){ 变种readyList = []; //存储一个参考的原始资料准备好方法。 VAR originalReadyMethod = jQuery.fn.ready; //覆盖jQuery.fn.ready jQuery.fn.ready =功能(){ 变参= [] .slice.call(参数); 如果(args.length&安培;&安培; args.length大于0&安培;&安培; typeof运算的args [0] ===“功能”){ readyList.push(参数[0]); } //执行原来的方法。 originalReadyMethod.apply(这一点,参数); }; //用于触发一切准备就绪事件 $ .triggerReady =功能(){ $(readyList)。每个(函数(我报){ 尝试 { el.apply(EL); }赶上(五){ 执行console.log(E); } }); }; })(jQuery的);

此外, page.php 我换成 get_header()和 get_footer() 功能如下:

&LT; PHP 如果(!使用isset($ _ REQUEST ['阿贾克斯'])){ get_header(); } ?&GT; &LT; PHP 如果(is_page()){ 的$ id = $ wp_query-&GT; get_queried_object_id(); $栏= get_post_meta($ ID,'sidebar_set“,真正的); $ sidebar_pos = get_post_meta($ ID,'sidebar_position“,真正的); } ?&GT; ... &LT; PHP 如果(!使用isset($ _ REQUEST ['阿贾克斯'])){ get_footer(); } ?&GT;

目前还有一些问题试图加载一个页面,革命滑块或Visual Composer的视差的内容,就像我们对视差或关于我们的网页为例。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 在JavaScript的jQuery库中操作AJAX的方法讲解【站长博客网】

    在JavaScript的jQuery库中操作AJAX的方法讲解【站长博客网】

    2017-04-09 16:00

  • 求赞,关于$this-success()方法的小技巧

    求赞,关于$this-success()方法的小技巧

    2017-04-08 12:01

  • PHP 实现ajax Loading加载功能

    PHP 实现ajax Loading加载功能

    2017-04-07 14:03

  • Ajax基础与登入教程

    Ajax基础与登入教程

    2017-04-06 16:04

网友点评