Body onload event after clicking on the link
Basically, I have a link on the page of my test.php page.
<a href="demo.php">click</a> <!-- test.php page --> Every time I want to call the on body function on the demo.php page, but only when I click on the link above, it will not be called when the page is refreshed.
<body onLoad="JavaScript:event(1,product,100);"> <!-- demo.php page --> You can achieve this by using hashtags in the URL, your HTML link:
<a href="demo.php#noCall">click</a> Javascript demo script:
<script type="text/javascript"> window.onload = function() { if (document.location.href.indexOf("#noCall") == -1) { //Does the hashtag NOT exist in the URL? alert("Event called when link is not clicked"); //Event called window.location.hash = ''; //Remove noCall from the hash for page-reload } }; </script> add a variable of type var first_time = true; , and then into your function, before you do anything, first check whether it was the first time or not: if (first_time == false) return false; After starting the function for the first time, set first_time to false .
Correction
The above solution will not work properly on the issue. The problem occurs when the page is refreshed. I am not deleting this answer as it can help other people see the differences between working solutions and this.