Search code examples
hyperlinkcycle

jQuery: how to trigger a click on document.ready only once?


I need to trigger a link, but only one time.

The problem is that the link loads the same page with some additional informations from a GET-function. So the page is in an endless reloading cycle.

This is my (dummy) code:

<a id="sendbutton" href="myurl.com/data1&data2">CLICK</a>

<script type="text/javascript">
   $(document).ready(function(){
      $('#sendbutton')[0].click();
   });
</script>

PS: This is related to a cron job. This event only has to occur once a day.


Solution

  • You could verify the URL, and if it contains parameters, do not trigger the link.

    <a id="sendbutton" href="myurl.com/data1&data2">CLICK</a>
    
    <script type="text/javascript">
       $(document).ready(function(){
       if(location.pathname == "" ){
       $('#sendbutton')[0].click();
       }
    });
    </script>
    

    I'm using the global variable "location" which store information about the current URL. Try this!