Jquery ajax results not displaying properly in IE - javascript

I have a site I'm working on which uses ajax to load page segments when js is available - this used to work in all browsers, the only problem being that the browser history was only updated in HTML5 broswers.
I have recently been doing a lot of work on the site and most recently decided to try to sort the history out for html4 browsers, in doing so I have checked the site in IE and found that a problem has developed with the way the data from some of the ajax calls is being displayed (only affecting IE - not FF).
The URLS are structured as follows:
sitename.dev/main_category/sub_category
if I just enter sitename.dev/main_category everything works fine but if I click the link for one of the sub categories at sitename.dev/main_category/sub_category the results are loaded but not displayed properly in IE. If I type the address into the browser for hard reload all works fine
WORKING (through hard reload):
Not working through ajax:
Because it is ajax loaded content I cannot inspect the dom, but my function appears to be firing correctly and the ajax call is returning the correct results.
Here is the code for my ajax call
function leftsort_click(event) { //main sort click (left menu)
var page = window.name, page_index,
api = $('#right_pane').jScrollPane({
showArrows: true,
maintainPosition: false
}).data('jsp');
if (!$(this).hasClass('sort_cat')) {
$('ul.sort_ul li, ul.cat_items li').removeClass('active');
$(this).addClass('active');
var sid = $(this).attr('id');
var title = $(this).html();
var loadUrlx= page;
if ((sid != '') && (sid != 'undefined')) {
loadUrlx += '/'+sid;
}
if($('.rpp_btn.active').length>=1){
var res_per_page = $.trim($('.rpp_btn.active').html());
page_index = $.trim($('.res_page_select.active a').html());
if (($('.rpp_btn.active').hasClass('just_clicked'))||(!$('.res_page_select').hasClass('just_clicked'))) {
page_index = '1';
}
if ((page_index != 1) || (res_per_page != 25)) {
loadUrlx += '/' + page_index + '/' + res_per_page;
}
$('.rpp_btn, .res_page_select').removeClass('just_clicked');
}
loadUrlx = b_url + loadUrlx;
if (History.enabled) {
History.pushState(null, null, loadUrlx);
}
$.ajaxSetup({
cache: false
});
$("#result_area").load(loadUrlx, function() {
$("#result_table").trigger("update");
api.reinitialise();
tsizer();
});
}
}
Any help or suggestions would be very much appreciated

worked this one out, i was injecting a div into a tbody - Ie's not happy about that.

Related

Trigger function on page change jQuery Mobile

I'm trying to run a function each time user changes 'page'in jQuery Mobile.
$(document).ready(function(){}
Doesn't work because all the pages are contained in the same .html.
The only way i can get it to work is if i listen on the nav buttons like this
$('#navNext').click(function() {
retrieveSession();
});
This causes a whole lot of head aches so i tried this
function retrieveSession(){
var activePage = $.mobile.pageContainer.pagecontainer("getActivePage");
var activePageId = activePage[0].id;
$( "#" + activePageId ).on( "pagechange", function() {
if (activePageId === "q1"){
alert("q1");
} else if (activePageId === "q2"){
alert("q2");
} else if (activePageId === "q3"){
alert("q3");
} else if (activePageId === "q4"){
alert("q4");
};
});
}
Then i run the function in $(document).ready(function(){}, the app still runs fine, but no alert when navigating through the 'pages'.
No exceptions either.
I have searched on here and can only find vague connections through other questions that don't help me with a solution.
What am i doing wrong here?

ajax image viewer , back button and history - missing html and css

I am playing with jquery and js, trying to build an ajax overlay image viewer for a PHP website. With this code included at the bottom of the 'gallery page', the viewer opens and i can navigate with next and previous links inside the viewer. But the back button and the history is hard to understand. The browser often shows only the response of the ajax call, without the underlying page and css files, after some clicks back.
Perhaps somebody knows what is generally happening in such a case? I would like to understand why back sometimes results in a broken page, i.e. only the ajax response.
<script type="text/javascript">
$(document).ready(function() {
function loadOverlay(href) {
$.ajax({
url: href,
})
.done(function( data ) {
var theoverlay = $('#flvr_overlay');
theoverlay.html( data );
var zoompic = $('#zoompic');
zoompic.load(function() {
var nih = zoompic.prop('naturalHeight');
var photobox = $('#photobox');
if($(window).width() >= 750){
photobox.css('height',nih);
}
theoverlay.show();
$('body').css('overflow-y','hidden');
$(window).resize(function () {
var viewportWidth = $(window).width();
if (viewportWidth < 750) {
photobox.css('height','auto');
zoompic.removeClass('translatecenter');
}else{
photobox.css('height',nih);
zoompic.addClass('translatecenter');
}
});
});
});
return false;
}
var inithref = window.location.href;
$(window).on('popstate', function (e) {
if (e.originalEvent.state !== null) {
//load next/previous
loadOverlay(location.href);
} else {
//close overlay
$('#flvr_overlay').hide().empty();
$('body').css('overflow-y','scroll');
history.replaceState(null, inithref, inithref);
}
});
$(document).on('click', '.overlay', function () {
var href = $(this).attr('href');
history.pushState({}, href, href);
loadOverlay(href);
return false;
});
});
</script>
edit
clicking forward works:
/photos (normal page)
/photos/123 (overlay with '/photos' below)
/locations/x (normal page)
/photos/567 (overlay with '/locations/x' below)
clicking back gives me the broken view at point 2.
Do you need to prevent the default behaviour in your popstate to prevent the browser from actually navigating back to the previous page?
you have to manage it by own code.
You have a few options.
Use localstorage to remember the last query
Use cookies (but don't)
Use the hash as you tried with document.location.hash = "last search" to update the url. You would look at the hash again and if it is set then do another ajax to populate the data. If you had done localstorage then you could just cache the last ajax request.
I would go with the localstorage and the hash solution because that's what some websites do. You can also copy and paste a URL and it will just load the same query. This is pretty nice and I would say very accessible
Changing to document.location.hash = "latest search" didn't change anything.t.
This goes into the rest of the jQuery code:
// Replace the search result table on load.
if (('localStorage' in window) && window['localStorage'] !== null) {
if ('myTable' in localStorage && window.location.hash) {
$("#myTable").html(localStorage.getItem('myTable'));
}
}
// Save the search result table when leaving the page.
$(window).unload(function () {
if (('localStorage' in window) && window['localStorage'] !== null) {
var form = $("#myTable").html();
localStorage.setItem('myTable', form);
}
});
Another solution is that use INPUT fields to preserved while using back button. So, I do like that :
My page contains an input hidden like that :
Once ajax content is dynamicaly loaded, I backup content into my hidden field before displaying it:
function loadAlaxContent()
{
var xmlRequest = $.ajax({
//prepare ajax request
// ...
}).done( function(htmlData) {
// save content
$('#bfCache').val( $('#bfCache').val() + htmlData);
// display it
displayAjaxContent(htmlData);
});
}
And last thing to do is to test the hidden field value at page loading. If it contains something, that because the back button has been used, so, we just have to display it.
jQuery(document).ready(function($) {
htmlData = $('#bfCache').val();
if(htmlData)
displayAjaxContent( htmlData );
});

Web App breaks on windows phone after page refresh

I am currently developing a web app that pulls metadata from a webservice.
It currently works in all browsers except that we get this weird issue on Windows Phone in Internet Explorer.
If you have a clear cache (first time load) it works with out a hitch, however once you refresh the page or navigate away and come back to the page the drop down lists fail to display data returned from the web service
Before:
After:
We are using standard jQuery $.ajax calls to a local webservice
It appears that in the after situation the success call back is being fired but the dropdowns aren't being rendered , and again this only happens after the page has successfully loaded once from a clean cache state and works fine in all other mobile browsers
the jquery code being used for the web service
function getAllPublications() {
$('.error').addClass('dn');
$('#selectYear').prop('disabled', 'disabled');
$('#selectVehicle').prop('disabled', 'disabled');
$('#selectManual').prop('disabled', 'disabled');
$('.manual-section').hide();
$.ajax({
url: "/_Global/HttpHandlers/OwnersManuals/GetPublications.ashx",
dataType: "json",
type: "GET",
success: function (data) {
$('.manuals-loader').hide();
if (data.getPublicationYearModelDataResult.ErrorCode == 0) {
allResults = data.getPublicationYearModelDataResult;
extractUniqueYears(allResults);
populateYearsDropdown();
} else {
$('.error.no-publication-error').removeClass('dn');
}
debugLog(JSON.stringify(data));
},
error: function (error) {
$('.manuals-loader').hide();
$('.error.api-error').removeClass('dn');
console.log(error);
}
});
}
function populateYearsDropdown() {
$('#selectYear')
.empty()
.append($("<option />").val('-').html(__pleaseSelect))
.removeAttr('disabled');
$.each(years, function (val, text) {
$('#selectYear').append($("<option />").val(text).html(text));
});
}
function extractUniqueYears(result) {
years = [];
if (result.PublicationYearModels != null) {
$(result.PublicationYearModels).each(function (i, item) {
if (item.YearModels != null) {
$(item.YearModels).each(function (j, subItem) {
var year = subItem.year;
if (!checkIfYearExists(year))
years[years.length] = year;
});
}
});
}
years.sort();
years.reverse();
}
Note: I have tried adding no cache and cache expiration headers to the page and also tried using cache expiration meta tags on the page as well with no effect
You should be able to trick browser into thinking that you're hitting the page for the first time using Location.reload(forcedReload). Here's an example using cookies to prevent you from getting stuck in an infinite refresh loop:
var ie_mobile_reload = false, FORCED_RELOAD = true;
if /refresh=true/.test(document.cookie) {
document.cookie = 'refresh=;'; /* Remove the refresh cookie */
ie_mobile_reload = true;
location.reload(FORCED_RELOAD);
}
/* IE Mobile only */
if /IEMobile/.test(navigator.userAgent) {
window.onbeforeunload = function() {
if (ie_mobile_reload) {
document.cookie = 'refresh=true'; /* Set the refresh cookie */
}
};
}
I set FORCED_RELOAD = true to skip the cache and cause the page to be reloaded directly from the server.
We ended up changing how the dropdown is initialized.
The problem was the dropdown list init was firing too late because of the caching issue on windows and wiping out the dropdown list content after it had been populated
we moved the logic into the DDL fill function and it fixed the issue so it clears all three DDLs when it is initially populated in the ajax function instead

'load' event not firing when iframe is loaded in Chrome

I am trying to display a 'mask' on my client while a file is dynamically generated server side. Seems like the recommend work around for this (since its not ajax) is to use an iframe and listen from the onload or done event to determine when the file has actually shipped to the client from the server.
here is my angular code:
var url = // url to my api
var e = angular.element("<iframe style='display:none' src=" + url + "></iframe>");
e.load(function() {
$scope.$apply(function() {
$scope.exporting = false; // this will remove the mask/spinner
});
});
angular.element('body').append(e);
This works great in Firefox but no luck in Chrome. I have also tried to use the onload function:
e.onload = function() { //unmask here }
But I did not have any luck there either.
Ideas?
Unfortunately it is not possible to use an iframe's onload event in Chrome if the content is an attachment. This answer may provide you with an idea of how you can work around it.
I hate this, but I couldn't find any other way than checking whether it is still loading or not except by checking at intervals.
var timer = setInterval(function () {
iframe = document.getElementById('iframedownload');
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
// Check if loading is complete
if (iframeDoc.readyState == 'complete' || iframeDoc.readyState == 'interactive') {
loadingOff();
clearInterval(timer);
return;
}
}, 4000);
You can do it in another way:
In the main document:
function iframeLoaded() {
$scope.$apply(function() {
$scope.exporting = false; // this will remove the mask/spinner
});
}
var url = // url to my api
var e = angular.element("<iframe style='display:none' src=" + url + "></iframe>");
angular.element('body').append(e);
In the iframe document (this is, inside the html of the page referenced by url)
window.onload = function() {
parent.iframeLoaded();
}
This will work if the main page, and the page inside the iframe are in the same domain.
Actually, you can access the parent through:
window.parent
parent
//and, if the parent is the top-level document, and not inside another frame
top
window.top
It's safer to use window.parent since the variables parent and top could be overwritten (usually not intended).
you have to consider 2 points:
1- first of all, if your url has different domain name, it is not possible to do this except when you have access to the other domain to add the Access-Control-Allow-Origin: * header, to fix this go to this link.
2- but if it has the same domain or you have added Access-Control-Allow-Origin: * to the headers of your domain, you can do what you want like this:
var url = // url to my api
var e = angular.element("<iframe style='display:none' src=" + url + "></iframe>");
angular.element(document.body).append(e);
e[0].contentWindow.onload = function() {
$scope.$apply(function() {
$scope.exporting = false; // this will remove the mask/spinner
});
};
I have done this in all kinds of browsers.
I had problems with the iframe taking too long to load. The iframe registered as loaded while the request wasn't handled. I came up with the following solution:
JS
Function:
function iframeReloaded(iframe, callback) {
let state = iframe.contentDocument.readyState;
let checkLoad = setInterval(() => {
if (state !== iframe.contentDocument.readyState) {
if (iframe.contentDocument.readyState === 'complete') {
clearInterval(checkLoad);
callback();
}
state = iframe.contentDocument.readyState;
}
}, 200)
}
Usage:
iframeReloaded(iframe[0], function () {
console.log('Reloaded');
})
JQuery
Function:
$.fn.iframeReloaded = function (callback) {
if (!this.is('iframe')) {
throw new Error('The element is not an iFrame, please provide the correct element');
}
let iframe = this[0];
let state = iframe.contentDocument.readyState;
let checkLoad = setInterval(() => {
if (state !== iframe.contentDocument.readyState) {
if (iframe.contentDocument.readyState === 'complete') {
clearInterval(checkLoad);
callback();
}
state = iframe.contentDocument.readyState;
}
}, 200)
}
Usage:
iframe.iframeReloaded(function () {
console.log('Reloaded');
})
I've just noticed that Chrome is not always firing the load event for the main page so this could have an effect on iframes too as they are basically treated the same way.
Use Dev Tools or the Performance api to check if the load event is being fired at all.
I just checked http://ee.co.uk/ and if you open the console and enter window.performance.timing you'll find the entries for domComplete, loadEventStart and loadEventEnd are 0 - at least at this current time:)
Looks like there is a problem with Chrome here - I've checked it on 2 PCs using the latest version 31.0.1650.63.
Update: checked ee again and load event fired but not on subsequent reloads so this is intermittent and may possibly be related to loading errors on their site. But the load event should fire whatever.
This problem has occurred on 5 or 6 sites for me now in the last day since I noticed my own site monitoring occasionally failed. Only just pinpointed the cause to this. I need some beauty sleep then I'll investigate further when I'm more awake.

JavaScript or jQuery browser back button click detector

Could someone please share experience / code how we can detect the browser back button click (for any type of browsers)?
We need to cater all browser that doesn't support HTML5
The 'popstate' event only works when you push something before. So you have to do something like this:
jQuery(document).ready(function($) {
if (window.history && window.history.pushState) {
window.history.pushState('forward', null, './#forward');
$(window).on('popstate', function() {
alert('Back button was pressed.');
});
}
});
For browser backward compatibility I recommend: history.js
In javascript, navigation type 2 means browser's back or forward button clicked and the browser is actually taking content from cache.
if(performance.navigation.type == 2) {
//Do your code here
}
there are a lot of ways how you can detect if user has clicked on the Back button. But everything depends on what your needs. Try to explore links below, they should help you.
Detect if user pressed "Back" button on current page:
Is there a way using Jquery to detect the back button being pressed cross browsers
detect back button click in browser
Detect if current page is visited after pressing "Back" button on previous("Forward") page:
Is there a cross-browser onload event when clicking the back button?
trigger event on browser back button click
Found this to work well cross browser and mobile back_button_override.js .
(Added a timer for safari 5.0)
// managage back button click (and backspace)
var count = 0; // needed for safari
window.onload = function () {
if (typeof history.pushState === "function") {
history.pushState("back", null, null);
window.onpopstate = function () {
history.pushState('back', null, null);
if(count == 1){window.location = 'your url';}
};
}
}
setTimeout(function(){count = 1;},200);
In case of HTML5 this will do the trick
window.onpopstate = function() {
alert("clicked back button");
}; history.pushState({}, '');
You can use this awesome plugin
https://github.com/ianrogren/jquery-backDetect
All you need to do is to write this code
$(window).load(function(){
$('body').backDetect(function(){
// Callback function
alert("Look forward to the future, not the past!");
});
});
Best
In my case I am using jQuery .load() to update DIVs in a SPA (single page [web] app) .
Being new to working with $(window).on('hashchange', ..) event listener , this one proved challenging and took a bit to hack on. Thanks to reading a lot of answers and trying different variations, finally figured out how to make it work in the following manner. Far as I can tell, it is looking stable so far.
In summary - there is the variable globalCurrentHash that should be set each time you load a view.
Then when $(window).on('hashchange', ..) event listener runs, it checks the following:
If location.hash has the same value, it means Going Forward
If location.hash has different value, it means Going Back
I realize using global vars isn't the most elegant solution, but doing things OO in JS seems tricky to me so far. Suggestions for improvement/refinement certainly appreciated
Set Up:
Define a global var :
var globalCurrentHash = null;
When calling .load() to update the DIV, update the global var as well :
function loadMenuSelection(hrefVal) {
$('#layout_main').load(nextView);
globalCurrentHash = hrefVal;
}
On page ready, set up the listener to check the global var to see if Back Button is being pressed:
$(document).ready(function(){
$(window).on('hashchange', function(){
console.log( 'location.hash: ' + location.hash );
console.log( 'globalCurrentHash: ' + globalCurrentHash );
if (location.hash == globalCurrentHash) {
console.log( 'Going fwd' );
}
else {
console.log( 'Going Back' );
loadMenuSelection(location.hash);
}
});
});
It's available in the HTML5 History API. The event is called 'popstate'
Disable the url button by following function
window.onload = function () {
if (typeof history.pushState === "function") {
history.pushState("jibberish", null, null);
window.onpopstate = function () {
history.pushState('newjibberish', null, null);
// Handle the back (or forward) buttons here
// Will NOT handle refresh, use onbeforeunload for this.
};
}
else {
var ignoreHashChange = true;
window.onhashchange = function () {
if (!ignoreHashChange) {
ignoreHashChange = true;
window.location.hash = Math.random();
// Detect and redirect change here
// Works in older FF and IE9
// * it does mess with your hash symbol (anchor?) pound sign
// delimiter on the end of the URL
}
else {
ignoreHashChange = false;
}
};
}
};
Hasan Badshah's answer worked for me, but the method is slated to be deprecated and may be problematic for others going forward. Following the MDN web docs on alternative methods, I landed here: PerformanceNavigationTiming.type
if (performance.getEntriesByType("navigation")[0].type === 'back_forward') {
// back or forward button functionality
}
This doesn't directly solve for back button over the forward button, but was good enough for what I needed. In the docs they detail the available event data that may be helpful with solving your specific needs:
function print_nav_timing_data() {
// Use getEntriesByType() to just get the "navigation" events
var perfEntries = performance.getEntriesByType("navigation");
for (var i=0; i < perfEntries.length; i++) {
console.log("= Navigation entry[" + i + "]");
var p = perfEntries[i];
// dom Properties
console.log("DOM content loaded = " + (p.domContentLoadedEventEnd -
p.domContentLoadedEventStart));
console.log("DOM complete = " + p.domComplete);
console.log("DOM interactive = " + p.interactive);
// document load and unload time
console.log("document load = " + (p.loadEventEnd - p.loadEventStart));
console.log("document unload = " + (p.unloadEventEnd -
p.unloadEventStart));
// other properties
console.log("type = " + p.type);
console.log("redirectCount = " + p.redirectCount);
}
}
According to the Docs at the time of this post it is still in a working draft state and is not supported in IE or Safari, but that may change by the time it is finished. Check the Docs for updates.
suppose you have a button:
<button onclick="backBtn();">Back...</button>
Here the code of the backBtn method:
function backBtn(){
parent.history.back();
return false;
}

Categories

Resources