Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep elements non-refreshed

The main goal is to keep non-refreshed the logotext <div class="small-7 medium-4 columns logo"> and the menu <nav class="pagedMenu" role="navigation">,without clipping on page refresh or while the content is loading from a page to another. Also, the menu state should be preserved from a page to another.

I've found here a possible solution that could solve the problem (you could use ajax to fetch the updated content and use jQuery to put the new content on the page and avoid the refresh entirely. Doing it that way, the existing data in the page would remain untouched. said @jfriend00)

So, I have tried to use an Ajax plugin (called AWS). In the AWS option page, I (suppose) that I've done the right thing pointing wrapper as Ajax container ID and also pagedMenu as Menu container class, Transition Effect Enabled, No ajax container IDs blank, no loader selected, having already a pulse loader implemented in the theme.

At this point, all I got it's a menu / side-menu (shiftnav) / pulse dot loader / content loading malfunction, generated perhaps by the wrong defined Ajax container id and/or menu container class(?) OR by a conflict with an existing JS / jQuery code, not so sure.

Also in Chrome console there is an error:

Uncaught SyntaxError: Unexpected token ; (anonymous function) @ ajaxify.js?ver=4.3.1:175 n.extend.each @ jquery-2.1.4.min.js?ver=2.1.4:2 n.fn.n.each @ jquery-2.1.4.min.js?ver=2.1.4:2 $.bind.$.ajax.success @ ajaxify.js?ver=4.3.1:169 n.Callbacks.j @ jquery-2.1.4.min.js?ver=2.1.4:2 n.Callbacks.k.fireWith @ jquery-2.1.4.min.js?ver=2.1.4:2 x @ jquery-2.1.4.min.js?ver=2.1.4:4 n.ajaxTransport.k.cors.a.crossDomain.send.b @ jquery-2.1.4.min.js?ver=2.1.4:4 

Everything is getting back to normal on page refresh but doesn't help at all, being useless.

I also have to mention that for the menu I've tried to keep the state using jQuery-Storage-API and storage=jQuery.sessionStorage; as you can see in mynewmenu.js file but that will not solve the non-refreshing elements problem.

The menu jsfiddle only, if this helps to have the whole picture, here thanks to @Diego Betto.

You can use this live link as example; there is a similar situation with the above described - Ajax implementation right(?) - and regarding the appearance, menu is kept non-refreshed from one page to another; if you browse Books, Works etc, menu sections you'll see; if there is a model that could be implemented here, I'll be glad to find it.

LE: meanwhile, I've tried another ajaxify solution made by @arvgta - special thanks - without success yet but as far as I've found from the Author, the defined elements should be div's with id's not classes. So, I'll try to find a way to modify somehow the code in order to have id instead on classes.

Also, I'll try to transform and implement in ajaxify.min.js file, the page-container element; jQuery('#page-container').ajaxify(); I'll come back with news.

LE2: I've tried to implement the solution using id's instead of classes but still, the pages are not loading correctly.

At this point we have ajax.min.js file updated with these lines:

(function($){    jQuery(document).ready(function(){     jQuery('#page-container').ajaxify({requestDelay:400,forms:false});   });  })(jQuery);  

Also, I've modified the theme file to have id=page-container instead if class=page-container.

In these conditions, on menu click, the links are changed (like it should), menu/ logotext elements seems to working almost fine (sometimes get skippy changing position), but the content is not loading correctly in all cases; Same here, everything is getting back to normal on manual page refresh (f5), but doesn't help.

LE3: It looks like the conflict is (at least) between Revolution Slider plugin and Ajaxify.

errormessage="Revolution Slider Error: You have some jquery.js library include that comes after the revolution files js include." ;="" +="
This includes make eliminates the revolution slider libraries, and make it not work." "<="" span="">"

Site live link here. Any thoughts / alternative in this area? (not interested in using other different platforms, different WordPress themes, etc. just a workaround in this existing situation)

LE4: As far as I can see, there are many users that voted up the Jake Bown answer that could be indeed a solution; but I can't find the reason that didn't work correctly implemented into my theme (without errors) live link here The elements logotext / menu are still fading on refresh, are not kept non-refreshed. Any thoughts @Jake Bown / anyone?

LE final. Buzinas delivered the closest answer for my needs, taking in consideration my site environment (plugins installed, etc).

like image 887
typo_ Avatar asked Sep 24 '15 20:09

typo_


2 Answers

From what you said I think I might have found the solution you're looking for - you want to load content dynamically whilst keeping the logo and navigation untouched? If so, this might be what you're looking for.

In the example, the pages are loaded in from a div within the page but could be used to load another URL or file:

$('.viewport ul li a').on('click', function(e) {    e.preventDefault();    var link = this.hash.substring(1, this.hash.length);    if($('.'+link).length) {      $('.viewport span.body').html($('.'+link).html());    } }); 
like image 127
Jake Bown Avatar answered Sep 30 '22 12:09

Jake Bown


TL;DR

I've created a plunker for you, take a look, and play with it as long as you can. You'll learn a lot from it!


I think you're trying too many things here, but didn't try the simplest:

The main goal is to keep non-refreshed the logotext and the menu ,without clipping on page refresh or while the content is loading from a page to another. Also the menu state should be preserved from a page to another.

If you want to do that, there are a few steps:

  • Create a 'master' page, that we're going to call index.html from now on.
  • So, our index must have the static part of our page, e.g menu, logo, footer etc.
  • Then, our 'subpages' must be cut down (no html, head, body, script, style tags, only the content that should be showed into our master page).
  • That done, now we must change our links to use AJAX instead of doing full refresh:

    /* we add a 'click' event handler to our menu */ document.getElementById('menu-menu-2').addEventListener('click', function(e) {   var el = e.target;    /* then, we see if the element that was clicked is a anchor */   if (el.tagName === 'A') {     /* we prevent the default functionality of the anchor (i.e redirect to the page) */     e.preventDefault();     /* we show our spinner, so the user can see that something is loading */     spinner.classList.remove('hidden');      /* and we call our AJAX function, passing a function as the callback */     ajax(el.href, function(xhr) {       /* we get our main div, and we replace its HTML to the response that came          from the AJAX request */       document.getElementById('main').innerHTML = xhr.responseText;       /* since the request was finished, we hide our spinner again */       spinner.classList.add('hidden');     });   } }); 
  • Ok, now our pages are already working via AJAX, and not reloading our static content.


But now, we see that we have some issues. For example, if someone tries to open one of our pages directly via URL, he'll see unstyled page, without the menu/logo etc. So, what should we do?

We have a few more steps now:

  • Simulate that our links are effectively transfering between pages using the History API:

    /* inside our ajax callback, we save the fake-redirect we made into the pushState */ ajax(el.href, function(xhr) {   document.getElementById('main').innerHTML = xhr.responseText;    /* save the new html, so when the user uses the back button, we can load it again */   history.pushState({     html: main.innerHTML,     title: el.textContent + '| neuegrid'   }, '', el.href);    /* (...) */ });  /* and outside it, we add a 'popstate' event handler */ window.addEventListener('popstate', function(e) {   /* so, if we've saved the state before, we can restore it now */   if (e.state) {     document.getElementById('main').innerHTML = e.state.html;     document.title = e.state.title;   } }); 
  • And we need that when the user enters directly to another page, e.g about-us, we redirect him to the index.html, and then load the about-us page from there.

  • So we create a redirect.js file, and we reference it in all of our subpages:

    /* save the page that the user tried to load into the sessionStorage */ sessionStorage.setItem('page', location.pathname); /* and them, we redirect him to our main page */ location.replace('/'); 
  • And then, in our index.html page, we see if there is any page in the sessionStorage, and we load it, if there is, otherwise we load our home page.

    var page = sessionStorage.getItem('page') || 'home'; /* we look into the menu items, and find which has an href attribute     ending with the page's URL we wanna load */ document.querySelector('#menu-menu-2 > li > a[href$="' + page + '"').click(); 

And that's it, we're done now. Take a look at the plunker I've been making to you.

And play with it as long as you can, so you'll learn a lot from it.

I hope I could help you! :)


Note: Just for reference, this is our ajax function:

function ajax(url, callback, method, params) {   if (!method) method = 'GET';    var xhr = new XMLHttpRequest();   xhr.open(method, url);    if (callback) xhr.addEventListener('load', function() {     callback.call(this, xhr);   });    if (params) {     params = Object.keys(params).map(function(key) {       return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);     }).join('&');     xhr.send(params);   } else {     xhr.send();   } } 
like image 25
Buzinas Avatar answered Sep 30 '22 12:09

Buzinas