473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to find browser back button event is fired?

139 100+
Hi All,

Is there any way to find the event is browser back button click?

I need to show an alert when user clicks on browser back button? I tried using the following code but alert is showing for every event on this page.
Expand|Select|Wrap|Line Numbers
  1. window.onbeforeunload = function(evt)   
  2.     {   
  3.     if (typeof evt == 'undefined')    
  4.     evt = window.event;    
  5.  
  6.  
  7.         if(evt)   
  8.         {   
  9.  
  10.         return "If you leave this page, your information will not be updated.";   
  11.                   }   
  12.     }  
  13.  
Sep 7 '09 #1
20 41197
RamananKalirajan
608 512MB
Hi bHappy,

In IE, you can find with the help of PageX, PageY but in Mozilla its difficult to findout the click of browse button. Instead you can use the window.onbeforeunload handler itself. You use this handler for only some pages where you want the user to save the datas are do something before he navigate to other page.
Sep 7 '09 #2
bhappy
139 100+
Hi,
Thanks for ur reply,
I am using this code in only one page where i had save, update, cancel buttons and some links also available in left side menu. My problem is alert msg is showing if i click any link or button also.

Plz some body help me...

Thanks.
Sep 7 '09 #3
Dormilich
8,658 Expert Mod 8TB
well, all these elements trigger this event (insofar the behaviour is correct). basicly pressing the back button is like using a link… maybe you can differentiate by the event target.
Sep 7 '09 #4
bhappy
139 100+
Hi,
Thanks for ur reply.
Can u provide me any examples or sample code.

Thanks.
Sep 8 '09 #5
Dormilich
8,658 Expert Mod 8TB
I would have to write some … don’t know when I’ve got time for that.
Sep 8 '09 #6
bhappy
139 100+
Hi,
I modified my code like this:
Expand|Select|Wrap|Line Numbers
  1. var links = document.getElementsByTagName('a');
  2. for (var i = 0; i < links.length; i++)
  3. {    
  4.     links[i].onclick = setGlobal;
  5. }
  6. function setGlobal()
  7.  {    
  8.     window.linkClicked = true;
  9.  }
  10. window.onbeforeunload = function() 
  11. {    
  12.     if (typeof window.linkClicked == 'undefined' || !window.linkClicked) 
  13.     {        
  14.     return "Are you sure?"       
  15.     }
  16. }
It is working fine If page contains only links and buttons. But in my page i had some links, buttons and links under lists(<ul>).

For the below code my javascript is working fine.
Expand|Select|Wrap|Line Numbers
  1. <input id="Submit1" onclick="setGlobal();" type="submit" value="submit" />
  2. <a href="#">Click Here</a>
My javascript is not working fo the below code
Expand|Select|Wrap|Line Numbers
  1. <ul>
  2.         <li><a href="/ConsoleManagement/default.asp">Place An Order</a></li>
  3.         <li><a href="/ConsoleManagement/history.asp">View Order History</a></li>
  4.         <li><a href="/ConsoleManagement/profile.asp">Update My Information</a></li>
  5.     </ul>
Plz any solution?
Sep 8 '09 #7
Dormilich
8,658 Expert Mod 8TB
when do you call the script? (if you call it before the elements are created by the browser, the event doesn’t get attached)
Sep 8 '09 #8
bhappy
139 100+
Few elements are creating after javascript, so my javascript is not working.
Thanks for ur idea.
Sep 8 '09 #9
Dormilich
8,658 Expert Mod 8TB
usually any such thing that is to apply to elements or nodelists of the document should be triggered by the window.onload event (because you can be sure, that then all the elements exist).
Sep 8 '09 #10
bhappy
139 100+
Hi,
Thanks for ur reply. Now my javascript is working fine. Other than this 'do u have any other solution'?

Thanks once again.
Sep 8 '09 #11
Dormilich
8,658 Expert Mod 8TB
at last I’ve found a way to differentiate between a hardcoded link and the back button. apply the onbeforeunload event (window), use the click event (link) to remove the onbeforeunload event. because the back button is not part of the document and the click event being triggered before the onbeforeunload event, clicking on the back button cannot remove onbeforeunload.
Sep 8 '09 #12
bhappy
139 100+
I could not able to understand what you are trying to say. Can you please explain me in detail. A piece of code will be better.
Sep 8 '09 #13
Dormilich
8,658 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. // execute onbeforeunload when leaving the page
  2. // by whatever means (links, buttons, back, forward, reload, …)
  3. window.onbeforeunload = function(evt)
  4. {
  5.     // do whatever you need here
  6. }
  7.  
  8. // get the links (I used only one for demo)
  9. var link = document.getElementById("test");
  10.  
  11. // remove the onbeforeunload event from the link
  12. link.onclick = function() { window.onbeforeunload = null; }
now your beforeonload function/code is only triggered by back, forward, reload.
Sep 8 '09 #14
bhappy
139 100+
Thanks for ur reply.
I have placed the javascript code at the bottom of the page.It is working fine, but my page will take 1 minute time to load, mean time if end user clicks back button then alert will not show, because javascript may/maynot be loaded.

If i place this code at the top of the page then for every event alert will show, because no controls are created yet.

Please provide me any solution.....

Thanks.
Sep 9 '09 #15
Dormilich
8,658 Expert Mod 8TB
what do you need to load for taking so long?
Sep 9 '09 #16
bhappy
139 100+
Actually my page is built by 2 different projects (using virtual directory). Header and footer is common for all pages in the site. I had totally 22 web sites which have same functionality. So business logic code is placed in one separate project and all 22 projects are refering this common project. so i need to place my javascript at the bottom of the page because in footer also some hyper links are exists. So my page is taking some time to load.

Note: Technology is 'ASP'. (Viryually link 2 projects).
Sep 9 '09 #17
Dormilich
8,658 Expert Mod 8TB
does that mean the server taking so long to compute some html or is it just a huge amount of pictures?
Sep 9 '09 #18
bhappy
139 100+
We are appreciating your patience.
All controls in the page will populate dynamically, because all websites use the same asp page. Based on the domain name contents will be retrived from database. so it is taking some time to load the page.
Sep 9 '09 #19
Dormilich
8,658 Expert Mod 8TB
then you should optimize your DB code. I never encountered such long delays (ok, I’m not having complex code, though)
Sep 9 '09 #20
bhappy
139 100+
Here Heder and Footer is populated from one project and remaning contents are populated by another project, so it is taking some time.
Sep 9 '09 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Liqun Xu | last post by:
Hallo NG, I created a Button with Click-Event dynamically: System.Web.UI.WebControls.Button bt_1 = new Button(); bt_1.Click += new EventHandler(bt_1_click); and I implemented the Funktion...
1
by: Bin Song, MCP | last post by:
Hi all I got a strange problem In my webform, I have a server-side Button "btnDisable" to save some data and enable or disable all other fields in this form based on the data. The logic is: on...
7
by: zdrakec | last post by:
Hello all: I note in my application, that when I use Server.Transfer("somepage.aspx"), when the new page is loaded, and I click the Back button on the browser, that the previous page, when it...
3
by: tshad | last post by:
Is there an event that gets fired when the Browsers Back Button is pressed? I want to save the clients entry if they should hit the back button by accident. Thanks, Tom
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
6
by: hemant.singh | last post by:
Hi all, I am trying to get a way by which I'll know exactly when user goes out of my site by clicking on close button in browser, So that w/e user click close button in browser, I can send a...
1
by: joe | last post by:
Logout question I use a session to store the login information, say Session("Login") = success and use page_load to test the session("Login") success or not say If Session("Login") <success...
3
by: axxon | last post by:
Hi guys! I am at my wits end detecting which one of the dymically loaded button fired the click event(since they all have same ID and text value) Heres the scenario: I have created a loop to read...
3
by: Gouri.Mahajan7 | last post by:
Hello, I am new to asp.net. Can anybody please tell me which event is fired when we close the browser. I want to remove some resources when the browser close button is pressed. Thanks in advance....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.