hi all, how to know user have opend window for firefox? i want to active my_function when user open the first window(firefox) ,but send or later window won't active my_function var opened; var count; function popup() { if(opened==null) { opened=my_function; alert('start------'); count=1; } else count++; alert(count); } window.onload=popup; Code (markup): is it have any problem with my code ? big thanks for helpful!
erm not sure if I get this, so you want to create a popup method that when run the first time, invokes a function but on any subsequent runs does not do so? http://www.jsfiddle.net/Y7ADm/ prototype <a href="#" id="opener">open popup simulation</a> HTML: var popup = { count: 0, firstUse: function() { alert("running this function"); }, open: function() { this.count++; if (typeof(this.firstUse) == "function") { this.firstUse(); this.firstUse = null; // destroy method } alert("this is our " + this.count + " run"); } }; document.getElementById("opener").onclick = function() { popup.open(); return false; }; Code (javascript):
i have modify your code as follows : var popup = { count: 0, firstUse: function() { alert("running this function"); }, open: function() { this.count++; if (typeof(this.firstUse) == "function") { this.firstUse(); this.firstUse = null; // destroy method } alert("this is our " + this.count + " run"); } }; window.addEventListener('load', function () {popup.open();}, false); Code (markup): actually , i want create a popup method that when run the first time (window for firefox), invokes "firstUse" function but when user open the second window and "didn't close the first window" then won't invokes "firstUse" function and only invokes open: function() . but your code is still invoke every times when i "open window (firefox)" ?
on the contrary, "running this function" only happens once, the function self-destroys after that. if you need to detect if a particular popup window is open and if not, open it, then that's a different case completely. this can give you some ideas... var popup = { features: "width=600,height=300,menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes", mypopup: false, // default handle, false (not object) title: "stickyPopups", _doOpen: function(url) { // internal method that opens it, don't call direct. sets mypopup to object if (!this._isOpen()) this.mypopup = window.open(url, this.title, this.features); }, _isOpen: function() { // internal check to see if popup is alive if (typeof(this.mypopup) == "object") { // this will return true even if it has been closed by the user so we use a try block to check body try { // note: this would only be effective if the popup url is local domain return this.mypopup.document.getElementsByTagName("body")[0] ? true : false; } catch(e) { // if we did not find the document body through a method, it's gone. return false; } } // if we got here, its not open yet / closed by .close() return false; }, open: function(url) { // public function to call. this.runOnOpen(); this._doOpen(url); }, close: function() { // custom control method that can close the popup if (this._isOpen()) { this.mypopup.close(); this.mypopup = false; } }, runOnOpen: function() { // run-once on open event callback if (!this._isOpen()) { alert("this code only runs when opening a brand new window"); } } }; document.getElementById("opener").onclick = function() { popup.open("http://fragged.org/dev/window.php?a=foo"); // need to use a local address here, you cant check dom/doc body otherwise due to xss permissions return false; }; document.getElementById("close").onclick = function() { popup.close(); return false; }; Code (javascript): <a href="#" id="opener">open popup simulation</a> <a href="#" id="close">close popup</a> HTML: see it run here: http://fragged.org/dev/window.php it won't do all you asked for, you need to refactor it to work with / track multiple window objects and re-init if they have all been shutdown to run the first open func again. good luck