skip to Main Content

When my index page is fully loaded, I want to run a JavaScript function when clicked anywhere. Once it works, it should no longer work. Just first boot and first click.

My function is Website2APK.showInterstitialAd();

My index page:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Clown Nights</title>
    <!-- Define the viewport -->
    <meta name="viewport"
          content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1.01">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta http-equiv="X-UA-Compatible" content="IE=10" />

    <!-- Include the HTML5 API files -->
    <script type="text/javascript" src="http://cdn.gameplayer.io/api/js/game.js"></script>
    <script type="text/javascript" src="http://cdn.gameplayer.io/api/js/developer.js"></script>

    <link rel="stylesheet" href="assets/game.css" type="text/css">
    <script src="js/globalconfig.js"></script>
    <script src="js/game.libs.js"></script>
    <script src="js/game.js"></script>
</head>

<body>
    <div id="gamediv"></div>
    <!--<div id="loader"></div>-->
    <canvas id="canvas" width="720" height="480" style="position: absolute; z-index: 1; display:block;"></canvas>
</body>
</html>

I tried the onload and onclick functions but I couldn’t.

2

Answers


  1. Chosen as BEST ANSWER

    I found the triggered function and fixed the problem.

    this.newGameBtn.addEventListener('click', function() { Website2APK.showInterstitialAd();});


  2. Simply add the event listener for click inside the onload event of window object. Then remove the click event listener inside the onclick function using document.removeEventListener(). It takes two arguments, first is the event-type i.e. click and second is the function that gets called on firing click event.

    window.addEventListener('load', () => document.addEventListener('click', handleFirstClick));
    
    function handleFirstClick() {
    //add  Website2APK.showInterstitialAd(); here
      console.log('clicked');
      document.removeEventListener('click', handleFirstClick);
    }
    div {
      height: 100dvh;
      background: teal;
    }
    <div id="gamediv">
      Some text!!
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search