son Posted January 20, 2010 Share Posted January 20, 2010 I have on one page a link that opens a pop up window to display file on server in nice format (it simply opens file with certain height and width). The link is as:<a href="page-1.php?lang=<?php echo $page_lang;?>" target="_blank" onClick="return popup(this, 'photoPage')">In link I pass on now a language option which should display the relevant 'Close window' link in correct language. The code for this is: if ($lang == 'en'){?><script type="text/javascript">document.write("<input type=\"button\" onclick=\"window.close()\" value=\"Close window\" />");</SCRIPT><?php}else{?><script type="text/javascript">document.write("<input type=\"button\" onclick=\"window.close()\" value=\"Fermer la fenĂȘtre\" />");</SCRIPT><?php}?> It simply displays an empty page. Do not get this. Is it not possible to use PHP in page that is opened with Javascript?Son Link to comment Share on other sites More sharing options...
jeffman Posted January 20, 2010 Share Posted January 20, 2010 It depends on what your popup() function does. If you are just clicking the link, the page will open like any page and be processed by PHP in the normal way.Why doesn't your PHP script output an entire HTML document? Outputting just a script could be a problem in some browsers. I'm not sure. And why output a script that uses document.write? Why not write the button in HTML, or have PHP echo the HTML for the button? Link to comment Share on other sites More sharing options...
son Posted January 20, 2010 Author Share Posted January 20, 2010 It depends on what your popup() function does. If you are just clicking the link, the page will open like any page and be processed by PHP in the normal way.Why doesn't your PHP script output an entire HTML document? Outputting just a script could be a problem in some browsers. I'm not sure. And why output a script that uses document.write? Why not write the button in HTML, or have PHP echo the HTML for the button?I thought that you can only use JavaScript to produce a 'Close window' button? The complete popup script is:function popup(mylink, windowname){if (! window.focus)return true;var href;if (typeof(mylink) == 'string') href=mylink;else href=mylink.href;window.open(href, windowname, 'width=860,height=760,scrollbars=no');return false;}function popup2(mylink, windowname){if (! window.focus)return true;var href;if (typeof(mylink) == 'string') href=mylink;else href=mylink.href;window.open(href, windowname, 'width=520,height=740,scrollbars=no');return false;} Son Link to comment Share on other sites More sharing options...
jeffman Posted January 20, 2010 Share Posted January 20, 2010 The popup functions look fine. So does your PHP. And when I loaded one of your scripts into a window, even without a complete document, it created a button.So I think the problem is somewhere else in page-1.phpJust to be clear: you do not need javascript to create a button of any kind. You do need javascript to execute window.open() Link to comment Share on other sites More sharing options...
son Posted January 21, 2010 Author Share Posted January 21, 2010 The popup functions look fine. So does your PHP. And when I loaded one of your scripts into a window, even without a complete document, it created a button.So I think the problem is somewhere else in page-1.phpJust to be clear: you do not need javascript to create a button of any kind. You do need javascript to execute window.open()I could not make my code working and changed it to:if ($lang == 'en'){echo '<script type="text/javascript">';echo 'document.write("<input type=\"button\" onclick=\"window.close()\" value=\"Close window\" />")';echo '</SCRIPT>';}elseif ... which works now.Thanks for your feedback. Still do not get it how a html button can close a window. Could you give me a quick sample code. Maybe then I understand what you mean...Son Link to comment Share on other sites More sharing options...
jeffman Posted January 21, 2010 Share Posted January 21, 2010 I think I understand your problem now. You kept talking about empty windows and buttons. That is not your issue, I think. It doesn't matter what makes the button. The issue is when are you allowed to close a window using javascript.A script may close a window that only if it opened that window. If Window A opens Window B, then Window A may also close Window B. But Window A cannot close itself, and Window B cannot close itself.This is because a window is not the same as a document. You own your own documents. But you only own a window if your document opened it. Otherwise, the user retains control of the window.A user would be unhappy if he surfed to a page and the window closed itself. All the window's history would be gone, and any state that existed in forms in previous pages would be lost, and so on. Link to comment Share on other sites More sharing options...
ShadowMage Posted January 22, 2010 Share Posted January 22, 2010 But Window A cannot close itself, and Window B cannot close itself.Isn't that what window.close() is for? Link to comment Share on other sites More sharing options...
jeffman Posted January 22, 2010 Share Posted January 22, 2010 Isn't that what window.close() is for?Try it in a variety of browsers and see. Link to comment Share on other sites More sharing options...
ShadowMage Posted January 22, 2010 Share Posted January 22, 2010 It works for FF 3.5 and IE 7&8 and Opera.I don't use any other browsers so I don't know if it works in anything else. Link to comment Share on other sites More sharing options...
jeffman Posted January 22, 2010 Share Posted January 22, 2010 We must be in different universes. I put this element in an empty document:<input type="button" onclick="window.close()" value="close me">click it, and the window does not close. FF3.57 and 3.6 throw an error. Safari and Chrome do nothing.From the HTML 5 specification: The close() method on Window objects should, if the corresponding browsing context A is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), and if the browsing context of the script that invokes the method is allowed to navigate the browsing context A, close the browsing context A (and may discard it too).The key words here: "auxiliary browsing context that was created by a script (as opposed to by an action of the user)"Meaning: your script can close a window only if the script opened the window. If your user opened it, no dice.Maybe older browsers still allow it. I don't know. Also, I'm on a Mac. Usually the development teams are at the same place, but I suppose there could be small discrepancies.But make no mistake. This restriction is the future, and browsers are moving there now. Link to comment Share on other sites More sharing options...
ShadowMage Posted January 22, 2010 Share Posted January 22, 2010 I have a script on PageA which opens PageB in a new window. I submit a form from PageB which takes me to PageC (same window as PageB). There is a script in PageC that closes the window.This works, though I'm not sure if that would count as being the same script. Perhaps it is? Link to comment Share on other sites More sharing options...
jeffman Posted January 22, 2010 Share Posted January 22, 2010 I just tried something similar and it also works as you report. Because your last window is ultimately a product of the first script, I think it meets the definition of "auxiliary browsing context" -- that is, your scripts opened it, so your scripts own it. You can close what you own, but not what you don't own.It would be different if your user got to that third page by entering the URI in the address bar. Same document, but a different browsing context.In a case like that, the user opened the window, so the user owns the window. It might be the 100th document to open in that window. It would be totally wrong for a script in document 100 to erase the history and state of 99 other documents.Maybe what is surprising is how much information a browser actually tracks. It's a lot. Link to comment Share on other sites More sharing options...
son Posted January 26, 2010 Author Share Posted January 26, 2010 I just tried something similar and it also works as you report. Because your last window is ultimately a product of the first script, I think it meets the definition of "auxiliary browsing context" -- that is, your scripts opened it, so your scripts own it. You can close what you own, but not what you don't own.It would be different if your user got to that third page by entering the URI in the address bar. Same document, but a different browsing context.In a case like that, the user opened the window, so the user owns the window. It might be the 100th document to open in that window. It would be totally wrong for a script in document 100 to erase the history and state of 99 other documents.Maybe what is surprising is how much information a browser actually tracks. It's a lot.In my case it is ulitmately the user's choice if he presses the button, so I should see no issues with this (it is also working now...)...Son Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.