Verify if the window is unlocked

let windowUrl = "";
let windowName = "my-window";
let size = "width=600,height=400";
let htmlContent = "<h1>Hello, World!</h1>";

let existingWindow = window.open(windowUrl, windowName, size);

if(existingWindow) {
   existingWindow.focus();
} else {
   let newWindow = window.open(windowUrl, windowName, size);
   newWindow.document.write(htmlContent);
   newWindow.document.close();
}

This code snippet is connected to an onclick event. It checks if a window already exists and either refocuses on it or creates a new window with specified content.

Answer №1

Consider trying the following approach:

<script type="text/javascript>
function loadNewPage(page) 
{
    if (opener && !opener.closed) {
        opener.focus();
    } else {
        var newWindow = window.open(page,'','width=800,height=600');
        opener = newWindow;
    }
}
</script>

For more information, check out this related question - JavaScript window.open only if the window does not already exist

You might also find helpful insights on the link shared by @Aram Kocharyan:
https://developer.mozilla.org/en/DOM/window.closed

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

JavaScript, XML, and PHP all support the use of HTML entities within their code

Having some trouble as a newbie again))) Can you please help me out, guys? I'm having an XML file with the following data: <Page> <Content>&lt;p&gt;Article content&lt;/p&gt;&#13; &#13; &lt;h1 style="font-style ...

Issue with AngularJS in Internet Explorer 7: "querySelector" property or method not supported by object

Incorporating angularjs into an existing asp.net project has presented a challenge due to the project's dependency on IE 7 compatibility mode. Upon running the project, an error from the angularjs file was encountered: Object doesn't support pro ...

Troubleshooting AngularJS: Issues arise when implementing ng-view

Here is the code snippet from my index.html file: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1.0 ,user-scalable=no"> <sc ...

Express: Improperly formatted MIME type for background graphics

Currently encountering an issue with displaying background images specified in my CSS. Upon checking the web inspector, the following message is logged: Resource interpreted as Image but transferred with MIME type text/html. The background image functio ...

Escape key does not close the modal dialogue box

I’ve customized the codrops slide & push menu (http://tympanus.net/codrops/2013/04/17/slide-and-push-menus/) to create an overlay on a webpage. Although it functions as intended, I’m struggling to implement a way to close it by pressing the escape ...

A peaceful WCF service initiates a client callback whenever a server update occurs

In the process of developing a WCF RESTFUL service on top of a c++ console application, I am confronted with an issue. The client accesses this c++ application through my restful wcf service using a browser. Every time there is an update received by my w ...

I am looking to insert a jQuery value or variable into the plugin options

How can I insert a jQuery value or variable into plugin options? This is my current script: $(document).ready(function() { // var bannerheight = 580; if ($(window).width() < 2100) { var bannerheight = 410; var opts = JSON.parse( ...

Differences in Performance Between jQuery data() and Regular Objects

I'm currently exploring the most efficient way to bind data for an object using jQuery data versus using a traditional object setup. I'm working on establishing a model for my app, and here is the code for the object: var PersonData = function ( ...

Create a form in a React component that allows the user to input information

My challenge is to create a functionality where each time the add button is clicked, it should dynamically add an input field that I can save. It's similar to this example, but with inputs instead. The complexity arises from the fact that the inputs a ...

Explore the data within a directory containing files and subfolders using javascript, HTML5, AngularJS, or jQuery

I'm looking to access and read files as well as subfolders within a folder that is selected using <input type="file"> I understand that I could potentially use <input type="file" multiple webkitdirectory />, but this solution is specific ...

Control the options of the Select menu using ajax

Consider having two dropdown menus <select id=first> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</opti ...

New update: Adjusting the chart by using the slider to modify the date values on the x-axis

Update: I have made changes to the question, the issue with values not appearing on the x-axis has been resolved. Now, as the slider is moved, the data on the graph remains constant and I can see changing x values. I am currently working on incorporating ...

Challenges with the jScroll Plugin

Trying to implement jScroll to load a partial view multiple times using an increasing page number. The partial view returns a few divs. To achieve infinite scrolling, the partial view needs to have a hyperlink tag that directs to the next page to load. Th ...

jQuery removes HTML tags from XML documents

I currently have a setup where a page is functioning as an AJAX loader with the help of jQuery. It is designed to retrieve XML documents, like the one shown below, and then place the values of title, authPhrase, and content into the appropriate div element ...

AngularJS and Select2's Multiple - Tags feature can display tags intermittently, showing some and hiding others as needed

Currently, I am implementing AngularJS along with select2 (not using ui-select). In my view, the following code is present: <select name="rubros" id="rubros" class="select2 form-control" ng-model="vm.comercio.tags" ng-options="rubro.nombre for rub ...

Is it possible for Jquery to directly retrieve the form input without the need for a SET button in the script?

I have a script that functions as a basic countdown. All you need to do is enter a number, press the SET button, and click START for the countdown to begin. Although I primarily use this script in the gym, I often forget to hit the SET button after enteri ...

Click to reveal additional images once the "show more" button is pressed

Hey there! I'm currently working on implementing a feature where, upon clicking "show more", 3 images will be displayed. And if the user clicks again, another set of 3 images will appear, and so on. I seem to be facing some difficulties with the JavaS ...

WebSocket - "Port is already being utilized"

I implemented the files provided in this article: into my codeigniter project, but I keep encountering an error: Message: socket_bind() [function.socket-bind]: unable to bind address [48]: Address already in use The JS section shows me: Connecting... C ...

Angular version 7.2.1 encounters an ES6 class ReferenceError when attempting to access 'X' before it has been initialized

I have encountered an issue with my TypeScript class: export class Vehicule extends TrackableEntity { vehiculeId: number; constructor() { super(); return super.proxify(this); } } The target for my TypeScript in tsconfig.json is set to es6: ...

Creating a calculator with JQuery and encountering challenges with performing multiple calculations

I've been working on a calculator using jQuery, but I'm encountering issues with the clear button not functioning properly after multiple calculations. On top of that, subsequent calculations are producing inaccurate results. I am seeking to enh ...