I load a javascript at the end of my html page with a function loginsucces(), which should be executed after a successfull login redirect to the page. It works perfectly in default browser mode(chrome), however when i load the page in incognito mode, it executes the function while the page is loaded the first time. Due to this behavior i got syntax errors because the php variables are not initialized yet. I know i can get around that somehow but i am curios to know why is the js function is executed in incognito mode while first-time page load and how can i avoid this?
<script>
function loginsuccess(){
if(!<?php echo isAuth() ?>){ return; }
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var json = JSON.parse(xhr.responseText);
...
}
}
xhr.open("GET","http://myurl.com/api/users/"+<?php echo currentUserId()?>,true);
xhr.send();
}
</script>