0

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.

Community
  • 1
  • 1
johann2002
  • 13
  • 2

1 Answers1

1

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.

Tim
  • 2,878
  • 1
  • 14
  • 19
  • 1 part Hello Tim, thanks you for your answer. I tryed out your advice, but it did not work out. Maybe I misunderstood which code into which function: I used the first script for the cookie (cookie code): function test(){ 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")); } – johann2002 Mar 29 '15 at 17:54
  • 2. part and then I moved it into the script block of the other functions. Than I called it from the checkCookie function. function checkCookie() { var user = getCookie("PAP"); test(); if (user != "") { } else { window.location.reload(); } } – johann2002 Mar 29 '15 at 17:54
  • Actually now looking at it I see why it was raw, because it was writing out a dynamic script tag. Which is awful. Forget the dynamic stuff, forget the function, forget the document.write. Add a script tag to your page for the URL using https with an id of pap-x6hetgh – Tim Mar 29 '15 at 18:02
  • 3. part The result was, that the page reloaded twoo times. The last time the page was blank and the html sourcecode info gave me only – johann2002 Mar 29 '15 at 18:02
  • I updated my answer now that I realized what the JS was doing. See above. Made it relative too, so no worries about http/https. – Tim Mar 29 '15 at 18:06
  • Unbelivable!It is working! I am new on stackoverflow, how can I give you the gold, silver and bronze badges? – johann2002 Mar 29 '15 at 18:25
  • Haha, glad to hear you got it working. Just use the upvote and the accepted answer, that plus the knowledge I helped out is good enough for me. – Tim Mar 29 '15 at 18:26
  • Well, sorry to tell you but I was wrong the main problem is not working :-/ The page is still endless reloading in chrome because the cookie is not set before the onload fires. Well I go bed now. I will find a solution tomorrow. Thank you again for your afforts. – johann2002 Mar 29 '15 at 18:50
  • No problem. Their code may frankly not work on chrome. Because as-is yours looks right. Start reviewing their linked code next to see if it is broken. You can wire up explicitly to window. Onload as well not using the body tag. – Tim Mar 29 '15 at 18:53