JS in the onload and OnUnload Magical Magic (reprint) __js

Source: Internet
Author: User
JS in the onload and OnUnload Magical Magic (reprint)

Introduction: This weekend is OK, think of the front of their own to do a business E-commerce platform, there are some places not perfect, think of perfect, well, the problem is this, in the E-commerce platform has such a scene, I put the shopping cart in the session, So that it can get the shopping cart model from the session during the whole shopping process, I'm in a shopping cart some of the goods in the database will reduce the number of purchases in their shopping cart, but if I close the window, how to add the number of items in the session's shopping cart model to the database, So I looked up Google, Baidu, get the first hint, is: Close the window automatically clears the session, so found the first method is to use the OnUnload property in <body> tags, call a js such as: <body onunload= " Close () > The method is to trigger the closing () event when the window is closed, so that I can define the method for deleting the session in the in-a () method ...

But this is not the case, when you refresh the page, and click on the link on this page onunload will trigger, so I again in Google, Baidu Search: The answer is as follows:

<script>
Window.onunload = function () {if (self.screentop>9000) alert (' This window has been closed. ')}
</script>
Or
<script>
Window.onunload = function () {if (self.screenleft>9000) alert (the window has been closed ...)}
</script>

Description

Window.screentop

Gets the y-coordinate of the upper-left corner of the browser client area relative to the upper-left corner of the screen

The number behind the screentop> must be greater than the height you display in the resolution

For example, 800*600, this number is more than 600.

Window.screenleft

Gets the x-coordinate of the upper-left corner of the browser client area relative to the upper-left corner of the screen

The number behind the screenleft> must be greater than the width you display in the resolution

For example, 800*600, this number is more than 800.

Usually these two values are set to 9000

So I used the above method to realize that the OnUnload event was triggered only when the page was closed.

Summary:

① when using the OnUnload attribute, you can use Ajax to clear the session, or you can use WINDOW.LOCATION.HREF to trigger a request, such as I'm using the struts2 I can use <body onunload= " javascript:window.location.href-' ${pagecontext.request.contextpath}/cart/closewindow.action ' ">

Then there is closewindow.action this request to deal with the product of the shopping cart in session, let the quantity add to the database;

② Here I also have Ajax to handle requests, but in fact we only have to process the session, do not have to process the session after the return of any asynchronous information, so I used to trigger a request method, the final wording is as follows:

<body

Onunload= "Javascript:if (self.screentop>9000) window.location.href= ' ${pagecontext.request.contextpath}/cart/ Closewindow.action '; ' >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.