5

I need to close the window after alertbox, I used the codes which was asked in Stack Question But my alert box is inside a php code, I am getting the alert box but once i close it the window is not getting closed, I am new to php . The codes are below, Please help me out guys

<?php 
$serial_get = trim(str_replace("(","",str_replace(")","",GetVolumeLabel("d"))));
 if ($serial_get == '1233-2AZ2'){ 
} 
else{
 echo '<script language="javascript">
window.alert("This is not a Licensed Software. Please contact IT Solutions.");
window.close()
</script>'; }?>
Community
  • 1
  • 1
Zain
  • 285
  • 2
  • 7
  • 23
  • This has nothing to do with php, all the code you need can be done in Javascript. Still, could you show us the entire result html of your page? – Jerodev Dec 17 '14 at 07:32
  • You can not pragmatically close window if this is not opened by program. – Amy Dec 17 '14 at 07:36
  • 1
    Quote from the answer from the question that you took the code `note that if you didn't open the window with Javascript yourself, you may not be authorized to close it depending on the browser.` – dav Dec 17 '14 at 07:37
  • @Jerodev: My point is that i definitely need that alert box inside php and also the windows needs to be close, This is my scenario. I actually put it inside a if else statement , and i have my c driver serial number which has to be validated in php if it is true then the page will run else the alert box will appear and the window should get closed. (Note : the serial number gets fetched from server) – Zain Dec 17 '14 at 07:38

2 Answers2

1

You need window.open(...) to be able to window.close(). You are using window.alert().

See Best Practice in the link https://developer.mozilla.org/en-US/docs/Web/API/Window.open

Ikmal Ezzani
  • 1,283
  • 8
  • 13
  • So can't we close a existing window after closing the alert box, Are you trying to say that?? – Zain Dec 17 '14 at 07:43
  • Not really, I'm trying to say that you can call `.close()` on a window that you open with `.open(...)`. Also I misunderstood what you trying to achieve with the .alert(). – Ikmal Ezzani Dec 17 '14 at 07:54
0

Some browsers won't respect the command unless it is user-initiated. But... Here's a workaround that may work for you. try this instead of close:

open(location, '_self').close();

Or maybe fool the browser into thinking it was user initiated. This may or may not work; haven't tested. I'm just throwing spaghetti at the wall...

var btn = document.createElement('button');
document.body.appendChild(btn);
btn.addEventListener('click', function() {
  open(location, '_self').close();
}, false);
btn.dispatchEvent(new Event('click'));
Todd
  • 5,314
  • 3
  • 28
  • 45