So i have 2 windows.. my main browser window (foo), then it has a href that calls a bit of JS that will make a mini window (bar) pop up.. The mini window contains a form which posts to itself and query a database and updates fields.. It works fine.
if($edit_type == "email")
{
if($update == true)
{
$form_email = $_POST['form_email'];
$queryreg = mysql_query("
UPDATE users
SET `email` = '$form_email'
WHERE username = '$username';
");
}
//Other code to run when the page hasnt been posted
}
My question is, when its submitted, and the DB is queried, would it be possible to close bar automatically and then refresh foo to see the updated data?
PS.
The script im using to open my popup is as follows:
<script language="javascript" type="text/javascript">
<!--
function myPopup(target)
{
window.open( target, "myWindow", "status = 1, height = 600, width = 600, resizable = 0" )
}
//-->
</script>
Thanks guys
It is possible to refer to the main window from your bar pop-up.
window.opener in this case is your foo.
if(window.opener && !window.opener.closed) { //Check whether opener is open
window.opener.location.href = window.opener.location.href ; //Refresh main
window.close(); //Close itself
}
Related
Can someone help me find the mistakes in my code? The window won't close when "no" is entered after finishing a game and I just can't seem to find the mistake.
For context: I was trying to make a battleship game and to make it interactive with javascript. When the ship is hit the user is supposed to have the option to play again (the window reloads just fine) and if the user decides that he doesn't want to play anymore he just has to enter "no" and the window closes but that doesn't work for some reason. I can't seem to find the mistake no matter what I do. Does anyone more experienced have an idea?
var table = document.getElementById("buttons");
var len_board = document.getElementsByClassName("tr").length;
var button = document.getElementsByClassName("location")
//Places ships on the board randomly
function placeShips(len_board) {
window.random_row = Math.floor(Math.random() * (len_board - 1)) + 1;
window.random_column = Math.floor(Math.random() * (len_board - 1)) + 1;
};
//Save users choice and determine if ship is hit
function getId(element) {
var chosen_row = element.parentNode.parentNode.rowIndex
var chosen_column = element.parentNode.cellIndex
if ((random_row == chosen_row) && (random_column == chosen_column)) {
element.style.backgroundColor = 'red';
element.style.borderColor = 'red';
element.textContent = "X";
alert("you hit my ship");
var answer = prompt("Would you like to play again? Enter 'Yes' for
yes and 'No' for no:");
var answer = answer.toLowerCase();
if (answer == "yes") {
location = location;
}
else if (answer == "no") {
window.close();
}
else {
var reply = prompt("I'm sorry, " + answer + " is not a valid
input. Please try again: ");
var reply = reply.toLowerCase();
if (reply == "no") {
window.close();
}
else if (answer == "yes") {
location = location;
}
else {
window.close();
};
};
}
else {
element.style.backgroundColor = "#a9c8f9";
element.style.borderColor = "#a9c8f9";
};
};
A script can only close a window that was opened by the script. You can't close the main window that loaded the script.
Window.close() on MDN
This method is only allowed to be called for windows that were opened
by a script using the window.open() method. If the window was not
opened by a script, an error similar to this one appears in the
console: Scripts may not close windows that were not opened by script.
This was done for security reasons. I'm sure you wouldn't like it if you were browsing the web and all of a sudden your window closed along with all the back history after clicking on a random link from a reddit post.
Closing the current window
In the past, when you called the window object's close() method
directly, rather than calling close() on a window instance, the
browser closed the frontmost window, whether your script created that
window or not. This is no longer the case; for security reasons,
scripts are no longer allowed to close windows they didn't open.
(Firefox 46.0.1: scripts can not close windows, they had not opened)
I am working on an asp.net web page which has a hyperlink. when ever that hyperlink is clicked, a new browser window is opened using javascript window.open. I want that If user clicks this link multiple times, then only one window is opened and not multiple windows. I just want that window to be highlighted when user clicks that hyperlink multiple times. Do I need to use window.open to detect if the url is opened in any other tab of the browser ? Is there any jQuery plugin built in for this so that I can use it for browser compatibility.
Here is the hyperlink url:
<a onclick="addClick()" href="javascript:void(0)">
New</a>
and here is the code I am using:
function addClick() {
var ID = jQuery("#ID").val();
var PSSWD = jQuery("#PSSWD").val();
var ACCID = jQuery("#ACCID").val();
var PASSWDINT = jQuery("#PASSWDINT").val();
window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI");
}
Please suggest.
Try
window.open("<url>", "<window name>");
This should always open in the same window. See reference.
HTML:
open window
var wins = {};
function openwindow(){
var url = this.href;
if(typeof wins[url] === 'undefined' || wins[url].closed)
wins[url] = window.open(url);
}
<script>
var windowObjectReference = null; // global variable
function openFFPromotionPopup() {
if(windowObjectReference == null || windowObjectReference.closed)
/* if the pointer to the window object in memory does not exist
or if such pointer exists but the window was closed */
{
windowObjectReference = window.open("http://www.spreadfirefox.com/",
"PromoteFirefoxWindowName", "resizable,scrollbars,status");
/* then create it. The new window will be created and
will be brought on top of any other window. */
}
else
{
windowObjectReference.focus();
/* else the window reference must exist and the window
is not closed; therefore, we can bring it back on top of any other
window with the focus() method. There would be no need to re-create
the window or to reload the referenced resource. */
};
}
</script>
click here
Check the reference https://developer.mozilla.org/en-US/docs/Web/API/window.open
To open only one instance of a popup window in an HTML page, use the windowName parameter of the window.open method.
For example
window.open('http://www.abc.com')
will open a new window each time the user clicks the link containing the window.open code.
In constrast,
window.open('http://www.abc.com','abc')
will open only one instance of the window, no matter how many times users click the link.
you can also use focus function as used below
<script language="javascript" type="text/javascript">
<!--
function popitup(url) {
newwindow=window.open(url,'name','height=200,width=150');
if (window.focus) {newwindow.focus()}
if (!newwindow.closed) {newwindow.focus()}
return false;
}
// -->
</script>
Edit 1
<a onclick="return addClick()" href="javascript:void(0)">New</a>
and here is the code I am using:
function addClick() {
var ID = jQuery("#ID").val();
var PSSWD = jQuery("#PSSWD").val();
var ACCID = jQuery("#ACCID").val();
var PASSWDINT = jQuery("#PASSWDINT").val();
window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI");
return false;
}
I have a jsp file which calls another jsp file opening it as a showmodal dialog window.
Say file1.jsp calls file2.jsp through file1.js.
File1.jsp-->File1.js (respective js files)
File2.jsp-->File2.js(respective js files)
Now to handleonclose in File2.jsp I added a function in File2.js.
When I hit close window but choose option as cancel, instead of just showing the old window.
It shows a modal window ontop of the existing modal window. Why is this happening. Am I missing something obvious.
What i expect to happen: When I choose Close but click cancel, nothing should happen.
File2.js function:
function handleOnClose() {
var resultsDoc = document.frames('searchBuffer').document;
if (event.clientY < 0) {
var bool = confirm('Are you sure you want to close the window ?');
if (!bool) { //Issue occurs here
window.showModalDialog("File2.jsp", "", "dialogWidth:1000px;dialogHeight:650px");
}
else {
resultsDoc.all('searchResults').innerText = '';
document.someSearch.submit();
}
}
window.returnValue = 'Discard';
}
Modified File2.js function:
function handleOnClose() {
var resultsDoc = document.frames('searchBuffer').document;
if (event.clientY < 0) {
var bool = confirm('Are you sure you want to close the window ?');
if (!bool) { //Issue occurs here
//*******removed the showmodal dialog window call
window.returnValue = 'Sorry';
}
else {
resultsDoc.all('searchResults').innerText = '';
document.someSearch.submit();
window.returnValue = 'Discard';
}
}
}
On the calling js (file1.js) I check if return value is "Discard" if so I refresh the page.
Otherwise I call the showmodal window again. My result is stored in the buffer so retrieval isn't a problem. Works like a charm
The following is in the NewForm.aspx which gets executed from a parent page (surveys/lists/testsurvey/allitems.aspx). The javascript works and open google. However, it opens the google in the pop up window (newform.aspx) but i need to close the popup window and show google.com on the parent window (or whatever the parent window link is)
<script type="text/javascript">
function redirect()
{
var inputcCtrls = document.getElementsByTagName("input");
for(m=0; m<inputcCtrls.length; m++)
{
if(inputcCtrls[m].type == 'button' && inputcCtrls[m].value == 'Finish')
{
var funcOnClick = inputcCtrls[m].onclick;
inputcCtrls[m].onclick = function () { window.location = "http://www.google.com/" }; }
}
}
redirect();
</script>
Use the opener property to find the window that opened the current window:
window.opener.location = "http://www.google.com/";
window.close();
I have an application that has a "parent" window. In the parent window there are menu items, like the following (using PHP here):
// sample link
echo "<li><a href=\"#\" onclick=openurl('covershift.php');>Shift Coverage</a></\
li>";
// logout link
echo "<li><a href=\"#\" onclick=openurl('logout');>Logout</a></li>";
Each link opens the appropriate page in a different "child" window. When the parent closes all of the child windows must close. I have implemented this functionality with Javascript, here is the function:
var childWindow = new Array();
var windowCount = 0;
function openurl(url)
{
if(url != 'logout') {
childWindow[windowCount]=window.open(url,'_blank','height=600,width=800,scr\
ollbars=1');
windowCount++;
if (window.focus) {
childWindow.focus();
}
} else {
var iCount;
for (iCount=0; iCount < windowCount; iCount++) {
if ((childWindow[iCount] != null) && !(childWindow[iCount].closed)) {
childWindow[iCount].close();
}
}
window.location='logout.php';
}
}
Here is my problem, when a user reloads the parent window and then clicks logout, the child windows remain open. This makes sense as the childWindow array is lost when the parent reloads.
How can I make the childWindow array persistent through a reload?
Thanks!
I don't think you can make a JavaScript object persist through a window load. Instead, could you make an unload event handler that closes your pages?
window.onunload = myCloseFunction;
function myCloseFunction()
{
// Just copying your code...
var iCount;
for (iCount=0; iCount < windowCount; iCount++) {
if ((childWindow[iCount] != null) && !(childWindow[iCount].closed)) {
childWindow[iCount].close();
}
}
}
Another option might be to have the child windows poll for the existence of the parent.
In the child window:
// Checks every 1 second for valid window.opener
var parentChecker = setInterval(function(){
if(!opener){
// Is this good practice? I don't know!
clearInterval(parentChecker);
window.close();
}
}, 1000);
You can save that array in the LocalStorage or in a cookie