Reputation: 13
It looks like the chrome browser uses onload/windows.onload like document.onload (see: window.onload vs document.onload )
This leads to the problem that chrome reloads the page too early and for that reason I have a loop on my page. Firefox and IE don't have this problem.
I have the following code to create a cookie (it is not my own code, it is from a software called 'Post Affiliate Pro'):
<script type="text/javascript">
<!--
document.write(unescape("%3Cscript id='pap_x6hetgh' src='" + (("https:" == document.location.protocol) ? "https://" : "http://") +
"www.example.com/papscript/scripts/trackjs.js' type='text/javascript'%3E%3C/script%3E"));//-->
</script>
I have to reload the page thats why I use the following code (I reduced the checkCookie code a little bit for better reading):
<script>
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("PAP");
if (user != "") {
}
else {
window.location.reload();
}
}
</script>
<body onload="checkCookie();">
I am looking for an idea how to solve the problem. With the onload-problem on chrome.
Upvotes: 0
Views: 1748
Reputation: 2912
Couple of things that jump out at me. Let's start simple, because that is the most likely issue.
You have js directly in the page, not tied to any event. This is a big antipattern because the exact timing of raw js is different for every browser, and never guaranteed. So let's start tackling your issue by fixing that.
Make a new script tag on your page like so, and remove the document.write code altogether.
<script id='pap_x6hetgh' src='//www.example.com/papscript/scripts/trackjs.js' type='text/javascript'> </script>
I bet you will find it magically works.
Upvotes: 1