Here are the steps to trigger a pop-up window in JavaScript that is filled with data fetched via an AJAX call:

I'm currently working on a java class where I need to insert a link into a table grid. This code is specific to the internal API of our company.

  /***MyCustomLink.java*********/

  customLink.setOnClick( "fetchURL", return false;);
  addGridItem("customLink");

  /***MyCustomLinkJSFPage.jsf****

  url= "www.example.com";

In the JSF page, I need to send an ajax request to the fetchURL function in JavaScript, which will trigger a popup window with a URL from the JSF page. Essentially, when the user clicks the link, a popup window should open using an ajax request. Any guidance on how to achieve this would be greatly appreciated.

Answer №1

My preferred method to replace popup windows is using a div element instead. This eliminates any issues with popup blockers in modern browsers. With a div "popup," there are no barriers.

To start, you must include the div in your HTML code. It can be placed anywhere on the page as it will serve as the area where your content appears. Make sure to assign an id tag (in this case, "popup") to the div.

<div id='popup' style='visibility: hidden; position: absolute; width: 500px; height: 300px;'></div>

Next, you need JavaScript code to handle requesting and receiving data from the server. Here's a basic function I use:

function fetchData(url, callback)
{
 var http;
 try { http = new XMLHttpRequest(); } catch (e) { try { http = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Failed to load!"); return false; } } }

 http.open("GET", url, true);
 http.onreadystatechange = function() { if(http.readyState == 4) { callback(http); } }
 http.send(null);
}

For demonstration purposes, let's have a button trigger the popup functionality so you can see how it works. Here's everything put together:

<html>
<head>
<script language='javascript'><!--

function fetchData(url, callback)
{
 var http;
 try { http = new XMLHttpRequest(); } catch (e) { try { http = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser broke!"); return false; } } }

 http.open("GET", url, true);
 http.onreadystatechange = function() { if(http.readyState == 4) { callback(http); } }
 http.send(null);
}

function openPopup()
{
 fetchData("url_of_popup_source.html", displayPopup);
}

function displayPopup(response)
{
 var popup = document.getElementById("popup");
 popup.innerHTML = response.responseText;
 popup.style.visibility = "visible";
}

function closePopup()
{
 popup.style.visibility = "hidden";
}

--></script>
</head>
<body>

<button onClick='openPopup();'>Click here</button>
<div id='popup' style='visibility: hidden; position: absolute; width: 500px; height: 300px; top:100px; left:200px;'></div>
</body>
</html>

This method is straightforward. You can customize the size and position of the popup as needed. The key benefit is that you avoid using traditional popup windows, which could potentially be blocked by browsers.

I also added an additional function called "closePopup" to hide the popup. Consider adding a button in your "url_of_popup_source.html" file that triggers this function when clicked. This allows users to view the information and then close the popup similar to a dialog box.

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

What causes the discrepancy in results between the quoted printable encoding operation in JavaScript and Oracle?

Programming Languages: // JavaScript code snippet //https://www.npmjs.com/package/utf8 //https://github.com/mathiasbynens/quoted-printable par_comment_qoted = quotedPrintable.encode(utf8.encode('test ąčęė')); console.log('par_comment_qot ...

The changes made to $scope in AngularJS are not being displayed in the view

The View Section <div class="col-sm-8" data-ng-init="init_enable_disable()"> <select class="form-control" style="margin-top: 5px;" data-ng-model="schedule.scheduleType" data-ng-change="enable_disableDiv(schedule.scheduleType);"> ...

Is there a way to deactivate the checkbox in an AngularJS input field?

<div ng-repeat="x in spaceutilization"> <input type="checkbox" name="{{x.filenumber}}" id="{{x.id}}" class = "pdffiles" value="101SP{{x.initials}}.dwg" /><label for="{{x.id}}"><button type = "button" class = "btn btn-primary btn-sm h ...

Adjust the size of an iFrame's content according to the dimensions of the iFrame

I am looking to use an iFrame for embedding a map, and my goal is to resize the map according to the values in the iFrame tag. Most tutorials I have found focus on auto-sizing the iFrame based on content, which is not what I need. Just to make it clear, I ...

Instructions for turning an HTML table cell into an editable text box

Essentially, I'm looking to enable users to click on the table and edit the text within it. I found inspiration from this Js Fiddle: http://jsfiddle.net/ddd3nick/ExA3j/22/ Below is the code I've compiled based on the JS fiddle reference. I tho ...

How can I create a React component that is accessible and controllable from external sources?

Attempting to create a Dialog component using React and Material-UI. Currently, the component behaves like a traditional Material-UI dialog with a button inside the class that opens the dialog. However, I aim to achieve the same behavior as the default Ma ...

What steps do I need to take in order to get my delete button functioning properly

Apologies for my lack of expertise in what may be considered a simple task... I'm currently developing a page with a table that allows users to add players to a game. Each <td> consists of a <input type='hidden'... and player infor ...

Could using 'require' in node.js lead to a memory leak issue?

I have been working on a program that experiences continuous heap growth. The program is quite simple - it repeatedly tries to load an external file (SyntaxError) using require. However, this external module fails to load due to a syntax error present in i ...

I am currently experiencing difficulties with jQuery version 1.7.2 when passing an AJAX params object with JSONP

Can someone help me with this script? I am trying to send service, data and content parameters but it's not working as expected. var addons = {}; $.ajax({ 'url': 'http://site.com/index.php', dataType:'jsonp' ...

Optimizing the Placement of Dynamic Google Maps

After setting up a responsive Google map using this jsFiddle and guidance from this stack overflow post, I encountered an issue. How can I keep the map centered on the marker across various viewports and browser sizes? I came across a solution in this res ...

Vue.JS component containing DOM elements outside of the designated $el scope

Transitioning from a custom front-end framework to Vue is a new adventure for me. Our website is gradually integrating Vue, and as we refactor old components, I've encountered an issue. My question is: Can a component create DOM elements outside of i ...

Enhancing the Calculator Functionality in a React Program

I'm struggling to incorporate a reset button into the input field, similar to CE on a calculator. I'm facing challenges when it comes to integrating it within the existing code structure. import { useRef } from "react"; import './A ...

Reorganize components in MongoDB record

Description: Each customer object includes a name field. A line object is comprised of the following fields: inLine - an array of customers currentCustomer - a customer object processed - an array of customers The 'line' collection stores doc ...

Transform buffer information into a visual representation

How can I convert the buffer data into an image so that when I loop through the results and render it in the img src, the user will be able to see the image? I am currently using ejs for rendering. <span> <img class="user-with-avat ...

Istanbul provides me with a thorough analysis, yet it always seems to conclude with an error

Currently, I am experimenting with a basic application (found in the Mocha tutorial code available at ) to troubleshoot why Istanbul is giving me trouble. The issue is that Istanbul successfully generates a coverage summary but then throws an error for unk ...

Submitting a Symfony 2 form with embedded file upload functionality using asynchronous JavaScript and XML (AJ

I've been working on creating an embedded file uploading form with file validation in Symfony 2. I found a helpful example for file uploading here and for embedded forms, I referred to this guide. Everything is working perfectly, but now I need to sub ...

Why am I unable to utilize an array in this manner in JavaScript, and what is the method for accessing the array using a calculated number?

var nodesXY = ['15% 15%','30% 16%','19% 42%','39% 80%',]; var number = ["0","1","2","3","4","0","0","0"]; //some loop AccesNodes(number[1]); function AccesNodes(number){ console.log(number); // ...

Retrieving PHP data, utilizing the split method to break apart the commas within the string, and then displaying the information within a div using AJAX for a continuous refresh cycle every 3 seconds

I am attempting to extract data from a PHP string like '10,58,72,15,4,723,' and split it by commas into arrays using the split() method. My goal is to then place these arrays into different div elements and update the data every 3 seconds. Below ...

Mapping JSON data containing a collection of objects as an observable, rather than as an observable array, is an

When developing my webpage, I needed to receive a viewmodel of a user account via Json. The data includes essential user details such as real name and email address. Additionally, I wanted to display (and modify) the groups that the user belongs to, along ...

Incorporate a CSS class name with a TypeScript property in Angular version 7

Struggling with something seemingly simple... All I need is for my span tag to take on a class called "store" from a variable in my .ts file: <span [ngClass]="{'flag-icon': true, 'my_property_in_TS': true}"></span> I&apos ...