Reputation: 373
Basically, I have link on my page test.php page.
<a href="demo.php">click</a> <!-- test.php page -->
I want to call function on body onload event on demo.php page everytime but only when click on link above then it would not be called on page refresh.
<body onLoad="JavaScript:event(1,product,100);"> <!-- demo.php page -->
Upvotes: 4
Views: 2383
Reputation: 9427
add a variable like var first_time = true;
, then in your function before doing anything first check if it is first time or not: if (first_time == false) return false;
After running the function for the first time set the first_time
to false
.
Correction
The above solution will not work as expected in the question. The problem happens when you refresh the page. I'm not deleting this answer as it could help some other people to see the differences between working solutions and this one.
Upvotes: 0
Reputation: 964
You can achieve this by using hashtags in the URL, your link HTML:
<a href="demo.php#noCall">click</a>
The demo.php javascript:
<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>
Upvotes: 4