Back to Blog

How to check if the user is on the homepage or specific page

You can use Javascript to check if the user is on the homepage or another specific page. In this example we will check if the user is on the homepage. IF, and only then we will automatically open the Web Widget after 5 seconds ant trigger a specific event.

To get the code working we will incorporate it by following 3 steps:

  1. Create a function to check if on homepage
  2. Use the function to check if on homepage
  3. Add delayed opening & trigger event

Javascript can also be used to check if the user is on a Desktop or Mobile device or you can Combine Functions to check if the user is one the homepage with a desktop device.

Desktop and Mobile Chatbot

1. Create function to check if homepage

Let’s start by creating a function to check if the user is on the homepage. To do that we can user the pathname. If on the homepage the pathname should be equal to “/”. Any other page will consist of additional characters in it’s pathname. If you want to include a specific page you can replace the “/” by the pathname of your page e.g. “/blog”.


<script>
  window.homepagecheck = function() {
    var check = false;
    if(document.location.pathname === "/"){
      check=true;
      }
    return check;
  }
</script>

2. Check if on homepage

The function is created and we can now call it. The function returns a boolean (true or false). The code below can be translated to something like: If it’s true that user is on the homepage then …

<script>

if(window.homepagecheck()){
            // add code here
          }

</script>     

3. Add delayed opening and start event

Now let’s add the delayed opening and trigger an event.

<script>

// Delayed Opening
setTimeout(function() {
   __flowai_webclient_app.open()
  }, 5000) // 5 secs
  
// Trigger Event
  window.__flowai_webclient_autoTriggerEvent = 'START_CHAT'

</script>


Find the complete code below

<script>
  window.homepagecheck = function() {
    var check = false;
    if(document.location.pathname === "/"){
      check=true;
      }
    return check;
  }


  if(window.homepagecheck()){
    setTimeout(function() {
      __flowai_webclient_app.open()
      }, 5000) // 5 secs
      
    window.__flowai_webclient_autoTriggerEvent = 'START_CHAT'
  }

</script>
Start for freeContact us