• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Window BeforeUnload and MS Edge

 
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just discovered that my following JS code will not fire when using MS Edge.
I where some thing else I can do to fire a function before the page unloads?

 
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to MDN, this event is supported by Edge. Does it work in other browsers?

Question: you are clearly using jQuery, why are you not using jQuery event handling?
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:According to MDN, this event is supported by Edge. Does it work in other browsers?

Question: you are clearly using jQuery, why are you not using jQuery event handling?



It works as expected in IE 11. However, my company gave directive to use MS Edge. That is when I discovered this no longer gets fired.
I started using this piece of code before I got into using jquery a long time ago.
I would be glad to use what ever is the better choice. Can you guide me to a jquery solution?
 
Bear Bibeault
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No testing on Chrome or Firefox?

https://learn.jquery.com/events/handling-events/
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:No testing on Chrome or Firefox?

https://learn.jquery.com/events/handling-events/



I use this method heavily in my code but what would I bind(on) to for handling the beforeunload event?
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:No testing on Chrome or Firefox?

https://learn.jquery.com/events/handling-events/



I got the following to work. But how can I incorporate my AJAX call?



This will not work:

 
Bear Bibeault
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try returning false to stop the event. If that doesn't work, call preventDefault on the event object (passed to the hander, see docs).
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Try returning false to stop the event. If that doesn't work, call preventDefault on the event object (passed to the hander, see docs).



Sorry to be such a bother, and thank you for your time.

I tried both and cannot get either to fire AJAX callback:



 
Bear Bibeault
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the handler code executing? What *is* happening?
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Is the handler code executing? What *is* happening?



When I use the return false. I get a prompt "Leave Site" dialog.

The e.preventDefault() seems to not fire function at all.
 
Bear Bibeault
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That last makes no sense: if the handler isn't being called, how would the preventDefault be executed?

What makes you think that the post isn't firing? Have you set a breakpoint to make sure the code is being executed? What sort of error handling do you have in case the post fails?
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:That last makes no sense: if the handler isn't being called, how would the preventDefault be executed?

What makes you think that the post isn't firing? Have you set a breakpoint to make sure the code is being executed? What sort of error handling do you have in case the post fails?



This is my servlet code. The Console does not display the message I have set when ran in MS Edge(or Chrome) but it does in IE 11:

 
Bear Bibeault
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you set a breakpoint?

Do the dev tools shows that the request was triggered? Did it succeed?

Pout a try/catch around the post to handle errors. Also you can add a done callback for the promise just to check if it is being called.
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Did you set a breakpoint?

Do the dev tools shows that the request was triggered? Did it succeed?

Pout a try/catch around the post to handle errors. Also you can add a done callback for the promise just to check if it is being called.



Set breakpoint, never reached.
Have try catch at servlet and JS, no error.
Set callback on AJAX, never called.
 
Bear Bibeault
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what you are saying is that the event handler is invoked only on IE and no other browser, correct?

That would be weird as it is supported on all browsers.

If so, then perhaps something at a higher level is at play. Is this code at the top-level, or within other code or handlers?
 
Marshal
Posts: 3355
492
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unrelated to your current issue - you could simplify this:
with something like this:
Also, it seems odd that you are creating a new instance of FileIndexer but not doing anything with it or keeping a reference to it.
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:So what you are saying is that the event handler is invoked only on IE and no other browser, correct?

That would be weird as it is supported on all browsers.

If so, then perhaps something at a higher level is at play. Is this code at the top-level, or within other code or handlers?



I am not saying the Event Handler is not invoked in MS Edge.
It is invoked but the AJAX part of the function is not being fired, just the return string part of the code.

This code was located within a callback function. I have moved it out to top-level position. Still no difference.
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:So what you are saying is that the event handler is invoked only on IE and no other browser, correct?

That would be weird as it is supported on all browsers.

If so, then perhaps something at a higher level is at play. Is this code at the top-level, or within other code or handlers?



I changed my code to add a delay and it works as expected:

 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:Unrelated to your current issue - you could simplify this:
with something like this:
Also, it seems odd that you are creating a new instance of FileIndexer but not doing anything with it or keeping a reference to it.



The FileIndexer class does several file cleanup functions. It does not return any values. Would this be where I would use interface in lieu of class?
 
Marshal
Posts: 26302
80
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:The FileIndexer class does several file cleanup functions. It does not return any values. Would this be where I would use interface in lieu of class?



Well, no, creating an object just so you can use side effects of the constructor to do something, and then discarding the object, that's just weird. Using a static method in that class would be a much more usual way of getting things done without creating an object.
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:So what you are saying is that the event handler is invoked only on IE and no other browser, correct?

That would be weird as it is supported on all browsers.

If so, then perhaps something at a higher level is at play. Is this code at the top-level, or within other code or handlers?



This seems to be a fix for IE11, Edge, Chrome:

 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Steve Dyke wrote:The FileIndexer class does several file cleanup functions. It does not return any values. Would this be where I would use interface in lieu of class?



Well, no, creating an object just so you can use side effects of the constructor to do something, and then discarding the object, that's just weird. Using a static method in that class would be a much more usual way of getting things done without creating an object.



To use a static method of a class will still require the new MyClass to access the static method wont it?
 
Bear Bibeault
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't have to do the conditional.

P.S. Why bother with IE? Even MS doesn't support it.
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You shouldn't have to do the conditional.

P.S. Why bother with IE? Even MS doesn't support it.



I understand, however, without the condition IE11 will not run the AJAX.
It will take some time before we can make sure all our computers have IE11 uninstalled as that has been our installed default.
 
Paul Clapham
Marshal
Posts: 26302
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:To use a static method of a class will still require the new MyClass to access the static method wont it?



If it's public then anything can access it.
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Steve Dyke wrote:The FileIndexer class does several file cleanup functions. It does not return any values. Would this be where I would use interface in lieu of class?



Well, no, creating an object just so you can use side effects of the constructor to do something, and then discarding the object, that's just weird. Using a static method in that class would be a much more usual way of getting things done without creating an object.



Can this be scoped to Application or just session?
 
Paul Clapham
Marshal
Posts: 26302
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:Can this be scoped to Application or just session?



No, you can only scope objects. But the code you posted didn't scope the objects you were creating so I don't see why that's a relevant question.
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Steve Dyke wrote:Can this be scoped to Application or just session?



No, you can only scope objects. But the code you posted didn't scope the objects you were creating so I don't see why that's a relevant question.



I just want to be clear. If I use static methods will they be bound to the session?
So if I call the static method with parameters and the properties of the class are based on a function using the parameters the properties will hold their value until I call the method with a different set of parameters?
And again all this will be at the session level?
Will this method help to reduce the java heap?
 
Bear Bibeault
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:
I just want to be clear. If I use static methods will they be bound to the session?


No. They will not be bound to any servlets scope. Just import the class containing the static methods and call them. They are just available in the classpath like any other Java program. No scopes needed.

So if I call the static method with parameters and the properties of the class are based on a function using the parameters the properties will hold their value until I call the method with a different set of parameters?


No. Parameters only "exist" while the function that they are passed to is executing. Just like any other Java program.

And again all this will be at the session level?

Again, no scopes.

 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Again, no scopes.



What about the java heap? Will it reduce since I am not having to use the new keyword to create an object of the class?
 
Paul Clapham
Marshal
Posts: 26302
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:What about the java heap? Will it reduce since I am not having to use the new keyword to create an object of the class?



I wouldn't expect any change because of that. Creating an object and not assigning it to any variable means that it can be garbage collected.
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Steve Dyke wrote:What about the java heap? Will it reduce since I am not having to use the new keyword to create an object of the class?



I wouldn't expect any change because of that. Creating an object and not assigning it to any variable means that it can be garbage collected.



So is the positive here just more correct(orthodox) coding?
 
Paul Clapham
Marshal
Posts: 26302
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Steve Dyke
Ranch Hand
Posts: 2083
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Yes.



I take it that this will not work:

 
Paul Clapham
Marshal
Posts: 26302
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure it will work, in the sense that "to work" means "to add that object as an attribute to that session". Assuming of course that there is a session available to add the object to, otherwise you'll get a NullPointerException. That doesn't guarantee that some other code somewhere else will behave the way you expected, though, if that's what you had in mind by "work".
 
And then the flying monkeys attacked. My only defense was this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic