# How to go back page when back button of android is pressed

**URL:** <https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815>\
**Category:** Uncategorized\
**Created:** [June 26, 2017, 4:02pm UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815 "2017-06-26T16:02:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [June 26, 2017, 4:02pm UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815/1 "2017-06-26T16:02:12Z")

</div>

How to go back page when back button of android is pressed using javascript

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 26, 2017, 4:13pm UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815/2 "2017-06-26T16:13:38Z")

</div>

You can listen to the **backbutton** event 🙂

```
document.addEventListener('backbutton', doSomething, false);

function doSomething(e) {
    // do something
    console.log("Back button pressed!");

    // prevent the default Android back button behavior!
    e.preventDefault();
}
```

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [June 27, 2017, 1:54am UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815/3 "2017-06-27T01:54:42Z")

</div>

how i apply this to web page ??

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [June 27, 2017, 10:53am UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815/4 "2017-06-27T10:53:56Z")

</div>

Please sort out this  
how to go index.html or any other page by back button of android device … please help u r my life line now

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 27, 2017, 3:06pm UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815/5 "2017-06-27T15:06:06Z")

</div>

You could add this code inside your `script` tags on the HTML page you are on. If you don’t have a page, you can try this: [https://www.kirupa.com/html5/html5\_starting\_template.htm](https://www.kirupa.com/html5/html5_starting_template.htm)

🙂

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [June 27, 2017, 3:26pm UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815/6 "2017-06-27T15:26:10Z")

</div>

please kirupa sort out my index.html problem by pressing back button of android device …please tell me the code

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 27, 2017, 3:29pm UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815/7 "2017-06-27T15:29:41Z")

</div>

I can’t do the work for you, since I don’t even know what the work is. The code I provided will allow you to check for the back button being pressed. If you want to next go to index.html, you can use the following:

`window.location.href = "index.html";`

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [June 27, 2017, 3:37pm UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815/8 "2017-06-27T15:37:42Z")

</div>

i exactly use this but how do i trigger it on back button

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 27, 2017, 3:43pm UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815/9 "2017-06-27T15:43:34Z")

</div>

When you click the back button on the device, the code will run. Is it not working when you try?

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [June 27, 2017, 3:45pm UTC](https://forum.kirupa.com/t/how-to-go-back-page-when-back-button-of-android-is-pressed/636815/10 "2017-06-27T15:45:16Z")

</div>

yes it doesnot work man when i hit the back button
