michelleisconfused michelleisconfused - 6 months ago 14
PHP Question

window.location not navigating to the correct location after page load is complete

When my website loads the javascript below it navigates to the correct anchor tag, but when the remainder of the page is loaded (other content via javascript + ajax) a split second later the page is pushed down way beyond the anchor tag. Is there anyway to call window.location (the javascript below) after all other php + javascript is loaded?

<script>
window.location = (""+window.location).replace(/#[A-Za-z0-9_]*$/,'')+"#<?php echo $windowlocation; ?>"
</script>

Answer

If you are loading via ajax, then you can presumably use the call back methods and some kind of method to check for when they are all done.

var sectionsLoaded = 0;
var numSections = 1; // number of sections you are loading

$.ajax({ 
    ...
    complete: function(){
        doneLoading();     
    }
});

function doneLoading(){
     sectionsLoaded++;
     if(sectionsLoaded == numSections) {
        //Do something
     }
}

Hopefully this answers your question, or is at least in someway useful. Also to be clear the PHP isn't loading. It is being executed on the backend and does nothing to the front end. (you might already know this just the way your question was worded was a bit confusing)

Comments