What are the ways to identify if a window has been refreshed or closed using JavaScript?

I've encountered this issue before, but I'm having trouble figuring it out.

In my .aspx file, there is a function in the onunload body tag that refreshes the parent page.

Now, I need to determine if the page has been refreshed or reloaded because the logic will call the function every time and close my "child" page.

Here is the code snippet:

<script type="text/javascript">
    function refreshparent(){
        window.opener.location.reload(true);
        window.close();
    }
</script>

This is what I would like to achieve:

<script type="text/javascript">
    function refreshparent(){
      if(page.wasclosed){
        window.opener.location.reload(true);
        window.close();
      }else{
   //do nothing
      }
    }
    </script>

Answer №1

Here is a suggestion you can consider:


window.onload = function () {
     if (!window.location.hash) {
         window.location = window.location + '#loaded';
         window.location.reload();
     }
}

After refreshing the page, the #loaded gets appended to the URL and once it's detected, the refreshing stops which means "true". You can then use search params to check if the page was reloaded. This logic can be implemented within a setTimeout function or any other suitable method.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Modifying state within a child component and utilizing the refreshed value in the parent component

I'm currently working on creating a simple header mini cart with a cart item counter in NextJS. I'm utilizing the form state value in the header component and then passing that value to the child components of the header where the numerical quant ...

Exploring ways to fetch recursive categories from MongoDB using Node.js or JavaScript

Here is a list of categories: https://i.sstatic.net/0PVhE.png Expected output: [ { "_id": "5f04bb4afce61722a8e3ca0f", "parentCategoryCode": null, "title": "Novelty", & ...

Display JavaScript and CSS code within an Angular application

I am working on an Angular 1.x application (using ES5) where I need to display formatted CSS and JS code for the user to copy and paste into their own HTML file. The code should include proper indentations, line-breaks, etc. Here is a sample of the code: ...

Identifying unclosed SQL connections

Recently, I came into possession of a significant ASP.net and SQL 2005 project. During my exploration, I discovered some unclosed SQL connections which is concerning. Is there a method to identify unclosed connections without having to manually review ev ...

Functionality of the copy button

I have a Bootstrap code snippet that I want to use to create an address and add copy button functionality: <div class="modal fade" id="bitcoinModal" role="dialog"> <div class="modal-dialog modal-lg"> & ...

Using AJAX to invoke a REST service endpoint

I'm currently implementing a REST service call using AJAX. $(document).ready(function () { var xmml = getXmlLoginRequest(); var wsdlURL = getWSDL('search'); $.ajax({ type: "POST", url: wsdlURL ...

Receiving a "Maximum call exceeded" error when using Vue.js router guards for the beforeEach hook

As I work on my Firebase-VueJS app, I am implementing basic security rules with router guards. In my main.js file, I have added the following code to handle permissions based on the user's authentication status. However, I encounter an error 'vue ...

Eliminating the 'white-space' surrounding concealed images

I am currently working on a project where I have a list of images that need to be hidden or shown based on the click event of specific <li> elements. While I have managed to achieve this functionality successfully, I am facing an issue with white spa ...

Managing two select fields in a dynamic Angular form - best practices

On my screen, I am dynamically creating elements using a reactive form. Specifically, I am creating cards with two selection fields each: https://i.sstatic.net/WUvQH.png Situation: When I add a card and choose a layout, the options for that specific layo ...

Create a dynamic animation of an image expanding and shifting towards the center of its parent container

Is it possible to animate an SVG image within a div to grow to 80% screen height and move to the center of the parent div when a specific function is called? If so, how can this be achieved? function openNav(){ $("#topnav").animate({height:'100%& ...

Tips for utilizing asp:image in the code-behind

I'm having trouble with rendering an asp:image in my code behind. Let me explain my approach: In the Default.aspx file, I simply have one label element. In the code behind, I create a string variable and populate it, then assign this value to the labe ...

jquery unclean data, in need of the jquery ui popup dialog

I have been experimenting with this library to determine if a form is dirty. By default, it triggers the standard browser confirmation asking whether you are certain about navigating away from the page. The jquery dirtyforms website includes a section sugg ...

Inline display malfunctioning

I am facing an issue while integrating ngTimepicker into my Angular project. It is functioning properly, but I am unable to inline the ngTimepicker inputs with the form input. Check out the plunker here I would like this section of HTML to be displayed in ...

Server unable to start and error message shown on command prompt

I am having trouble opening the webpage from the code hosted on Github. When I try to run the server, I encounter errors that are displayed in the command prompt as shown below: Code link: https://github.com/mschwarzmueller/nodejs-basics-tutorial/tree/mas ...

Exploring MongoDB's Aggregation Framework: Finding the Mean

Is there a way to use the Aggregation Framework in MongoDB to calculate the average price for a specific Model within a given date range? Model var PriceSchema = new Schema({ price: { type: Number, required: true }, date: { ...

Updating the ghost image for HTML5 drag and drop functionality

I am facing an issue with a large div element that is too big for my liking. I have come up with a solution to change it by implementing the following code: <div draggable = "true" id = "ololo"> </div> var k = document.getElementById("ololo"); ...

Angular encountering issue with HTTP service not functioning as expected

I have been encountering issues while trying to retrieve data using JSONPlaceholder in my service. Despite my efforts, I have not been successful in fetching any data. I would greatly appreciate any assistance in resolving this matter. user.component.html ...

Uncaught jQuery onchange event not firing

I am facing an issue with a drop-down list inside a data grid on the main page. When values are changed, a popup should display and the values from the popup need to be sent back to the main page. However, I am having trouble triggering the onchange event. ...

Identify the most suitable HTTP method within a reusable function for making API requests with Angular's HTTPClient module

I am working on implementing a reusable service to handle requests to my API. Currently, it is functioning as expected, but only for GET requests. This is the current function in use: makeAPIRequest = ({ ...opts }) => { return this.http.get(opts ...

Enhance the performance of ASP.NET application on mobile browsers like IPhone and Android for optimal user experience

Working on an ASP.NET application (not MVC) and looking to optimize it for IPhone and Android browsers. Have a few questions: Is there a way to detect IPhone/Android when users access the site? Any tips or pitfalls to be aware of when developing ASP.NET ...