Using JavaScript to open a window in ASPX page

When I use the code below to open a new window and receive a success message, I also want to close the current window. However, I am encountering an error:

Error: newwindow is not defined

 var value = getQueryStrings();
 var cust_id = value["cust_id"];
 try {

     PageMethods.add_plan_items(unescape(items), cust_id, device, OnGetMessageSuccess, OnGetMessageFailure);

 } catch (ex) {
     alert(ex);
 }

 function OnGetMessageSuccess(result, userContext, methodName) {
     try {
         var ret_array = result.toString().split(':');
         if (ret_array[0] == "Success Alert ") {

             var left = (screen.width / 2) - 1015 / 2;
             var top = (screen.height / 2) - 550 / 2;

             self.close(); //Close the current window

             //Code to be executed after closing the current window

             var newwindow = window.open('test.aspx?id=Y&cust_id=' + cust_id, 'Add Items', 'height=550,width=1000,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,left=' + left + ', top=' + top + ',screenX=' + left + ',screenY=' + top + '');
             if (window.focus) {
                 newwindow.focus()
             }
             return false;
         } else {
             self.close(); //Close the current window
             alert('Error Alert : Error for adding plans in your specified device. Please try again later!');
         }

     } catch (ex) {
         alert(ex);
     }

 }

 function OnGetMessageFailure(error, userContext, methodName) {
     alert(error.get_message());
 }

Answer №1

It's important to follow a specific order when working with parent and child windows in JavaScript.
Before closing the parent window, make sure to save it first. Then open the child window (new window) and finally close the parent window.

var main = self.window;
var popup = window.open(...);

main.close();

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

Disabling JavaScript for a particular page using Selenium:

My current challenge involves navigating to a webpage, locating a link to another page within it, and proceeding to that link without using javascript. One approach I've considered involves the following method: options = Options() options.add_exper ...

Two separate occurrences hold identical values

I'm encountering an issue where instantiating a class two times results in the second instance retaining parameters from the first instance. Here's a simple example: var Test = function() {}; Test.prototype = { bonjour: null, hello: { h ...

Discover results while inputting in the search box

Can anyone assist me? I'm looking to enhance my current code by adding a search functionality to the ul list. I want users to be able to type in a search term and have the matches automatically highlighted as they type, similar to a "find-as-you-type ...

Having trouble with connecting to Gmail while using Nodemailer? Facing a Connection Timeout

Seeking assistance with sending emails through nodemailer. See below for the code snippet. var transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: '<<a href="/cdn-cgi/l/email-protecti ...

Tips for detecting web elements using Selenium within an iframe with Javascript

https://i.sstatic.net/9q3yq.pngWhen attempting to automate a wiki page using Selenium WebDriver, I am encountering difficulties identifying web elements within an iframe. dri.switchTo.frame(frameid); This is the iframe in question: <iframe frameborde ...

Perform Function Again

Recently, I came across some code for a tesseract ocr from Google that seemed to be functioning well. However, I encountered an issue where it would work fine the first time I input a URL, but on the second run, it wouldn't work without a manual page ...

The jQuery window.load() function fails to execute on iOS5 devices

I added a basic loading div to my website that fades out once the entire page has finished loading. The code I used is simple: $(window).load(function(){ $('#loading').fadeOut(500); }); While this works well on all desktop browsers and Chro ...

An error was encountered: Component.setState is undefined and cannot be read, leading to a TypeError

Could the error be caused by the fact that the Counter component remains at a count of 0, with this not bound anywhere? class Counter extends React.Component { constructor(props) { super(props) this.state = { count: 0 ...

Utilizing a foundational element to automatically unsubscribe from multiple observable subscriptions

Within our Angular application, we have implemented a unique concept using a Base Component to manage observable subscriptions throughout the entire app. When a component subscribes to an observable, it must extend the Base Component. This approach ensures ...

Showing button based on a particular value

I am trying to dynamically display a button based on the value of the sendSMS property for the logged-in user. I have added this property in the viewer model, which is connected to the user's base model. However, I am encountering difficulties with us ...

I am facing issues connecting my Express Node server to my MongoDB database using Mongoose

Starting my backend journey, I keep encountering the same error with my server.js --> // Step 1 - Create a folder named backend // Step 2 - Run npm init -y // Step 3 - Open in VS Code // Step 4 - Install express using npm i express // Step 5 - Create se ...

Error: Unable to access the 'collection' property as it is undefined in the MongoDB connection

I am having trouble connecting to the MongoDB database and receiving the following error: An error occurred TypeError: Cannot read properties of undefined (reading 'collection') at C:\Users\Joh\Desktop\chipsproject\s ...

Using StencilJS to Incorporate CSS/SASS Styles from node_modules

I'm currently facing a challenge in importing a CSS file containing variables from a node_modules package. Despite trying to replicate the process outlined in stencil.config.ts, the builds continue to end up in a different location than intended, leav ...

Change a JSON Array into an HTML string that is wrapped using an Angular service

Currently, I am attempting to integrate a wysiwyg editor into my Angular application. The objective is to retrieve an array of objects by querying an endpoint, then format the data within each object based on its property name with different HTML tags. Fin ...

The AngularJS filter may encounter issues if the filter's model is located in a separate view that shares the same controller

I am facing an issue with using a filter in my second view while having two views that share the same controller "TabsController as TabsVM". The filter I need to use is "item in items | filter:modelName", where modelName is located in the input field of th ...

Linking two directives in AngularJS to the scope models of a parent controller

In my form controller, I have 2 object models: model1 - {name:"foo"} and model2 - {name:"model2"}. To implement this, I created 2 directives with isolated scopes. One directive binds only to elements and uses model1, while the other binds only to attribute ...

Having trouble with the `npm start` command while working with react.js?

Currently, I am in the process of setting up React.js. To achieve this, I executed npm install -g create-react-app followed by create-react-app my-app. Afterward, I proceeded to run the npm start command but encountered the error displayed below. https:// ...

AmplifyJS is throwing an error: TypeError - It seems like the property 'state' is undefined and cannot be read

I am currently working on integrating the steps outlined in the Amplify walkthrough with an Angular cli application. My app is a brand new Angular cli project following the mentioned guide. My objective is to utilize the standalone auth components a ...

How to extract the complete QueryString from a URL using ASP.NET

If you have an URL in this specific format: http://www.mysite.com/login.aspx?ref=~/Module/MyPage.aspx?par=1&par2=hello&par3=7 The content of the QueryString is used to redirect the user back to the page they were on before logging in. To retain t ...

Utilize JavaScript to redirect based on URL parameters with the presence of the "@ symbol"

I need help redirecting to a new page upon button click using a JS function. The URL needs to include an email address parameter. <form> <input type="email" id="mail" placeholder="ENTER YOUR EMAIL ADDRESS" requir ...