10

Hi all i am developing a chat application ... i have multiple chat windows ... i want to know which windw contain new message ... i have the following code ..

function getCount()
{
    $.ajax({
       type: "POST",
       url: baseUrl + '/Chat/count',
       data: "chat_id=" + document.ajax.chat_id.value,
       success: function(msg){
                if(msg == 'new1') {
                    self.focus();
                                            //window.focus();

                }
            }
    });
}

If an operator attending both chat....

for example the url is like http://localhost/nisanth/admin/Chat/index/chatId/15 http://localhost/nisanth/admin/Chat/index/chatId/16

http://localhost/nisanth/user/Chat/index/chatId/15 http://localhost/nisanth/user/Chat/index/chatId/16

if the user 16 enter a message i need focus

http://localhost/nisanth/admin/Chat/index/chatId/16

This code is work fine with IE but not in firefox...please give me a solution... the above code is in the same html

Nisanth Kumar
  • 5,667
  • 8
  • 33
  • 43
  • 1
    I don't see how that can work in IE; `self` is undefined in the quoted code. If it's defined elsewhere, best to show that since it's kind of important to know what it's meant to reference. – T.J. Crowder Mar 28 '10 at 14:55
  • How does "...i want to know which window contain new message..." relate to `focus` not doing what you expect? (Also, can you explain more what you mean by that?) – T.J. Crowder Mar 28 '10 at 14:57
  • @Nisanth: `window` may be, but again, not `self` unless you're defining it somewhere else. `self` is not a built-in object. It's the name a lot of people use inside a closure they're passing as a callback/event handler when they want to refer to the `this` value from within the closure, but to do that, you have to actually define it (e.g., `var self= this;`). – T.J. Crowder Mar 28 '10 at 14:57
  • self.focus() is working in IE8 – Nisanth Kumar Mar 28 '10 at 15:13
  • Well whatever, @Nisanth; it's irrelevant to the question of what will work in Firefox, because in Firefox it's under user control. – Pointy Mar 28 '10 at 15:16
  • @Pointy is there any trick to handle my issue in firefox – Nisanth Kumar Mar 28 '10 at 15:22
  • Read my answer, @Nisanth. You cannot override the user configuration. – Pointy Mar 28 '10 at 15:29
  • @T.J. Crowder: Most browsers implement a global variable named `self`, which is simply a reference back to the global object, is not part of any specification but seems that it exist on every browser... (`window.self == window`) https://developer.mozilla.org/En/DOM/Window.self – Christian C. Salvadó Mar 28 '10 at 16:25

3 Answers3

13

Firefox will only obey requests to raise a window if a security option is set, and it's not set by default. Chrome won't pay attention to focus() requests at all, as far as I can tell. Safari does obey focus() request.

The specific Firefox setting is in the "Tools" -> "Options" ("Edit -> Preferences" on Linux, maybe MacOS) dialog. There's a "Content" tab, and in that there's a checkbox for enabling Javascript. Along with that is an "Advanced" button that brings up another dialog, wherein one finds a checkbox to allow (or disallow) the raising and lowering of windows by page code.

edit: Here is a test page: http://gutfullofbeer.net/focus1.html and you should be able to see that Firefox will raise a window when the page calls window.focus(). You must either have the browser set up so that new windows (created with window.open()) open up in a new separate window instead of a tab, or else you can tear off the tab of the secondary page when it opens.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Well then you must be doing something wrong, because it definitely works. Have you tried issuing an "alert()" when you think that your call to "window.focus()" is taking place? – Pointy Mar 28 '10 at 15:44
  • alert is working, self.blur(), working, window.moveTo() these all r working :(... if alert() is working then the window is get focused but my client don't want that ;) – Nisanth Kumar Mar 28 '10 at 15:49
  • I have used window.focus() in my own applications, and I am 100% certain that it works in Firefox when the browser is configured to allow it. – Pointy Mar 28 '10 at 15:50
  • your example is working fine here... but i think my case have some issue...:( is there any way to alert window object.... – Nisanth Kumar Mar 28 '10 at 16:09
  • Uhh ... well, I'm not sure what you mean; the "window" object is built into the browser. Now it's possible that something has *changed* the `focus()` function, but that does not seem very likely. – Pointy Mar 28 '10 at 16:20
  • 3
    Sadly this answer is no longer relevant as the preferences window has changed dramatically and the setting is no longer found in there. – Thomas Paine Aug 28 '18 at 17:23
  • 1
    @ThomasPaine ancient Stackoverflow questions are an interesting problem. – Pointy Aug 28 '18 at 22:00
1

I had the same problem, i though there was a problem with my JS script, after a long search i have found a solution:

1) In a new tab, type about:config in the address bar and press button "Enter". Click the button "I accept the risk!" in order to confirm the warning

2) In the search field, type dom.disable to get dom.disable_window_flip

3) If the property dom.disable_window_flip is true, double-click it to switch the value from true to false

Shessuky
  • 1,846
  • 21
  • 24
0

For anyone else looking to focus on a tab, another tab (tab A) can bring a different tab (tab B) to the front.

If the window.name of tab B is 'myWindow123', then in tab A run this:

window.open('', 'myWindow123');

If you want to re-focus on the tab that opened you, run:

window.open('', window.opener.name);
Glen Little
  • 6,951
  • 4
  • 46
  • 68