我想在用戶點擊一個鏈接時用javascript打開一個新標籤(或窗口,取決於用戶的瀏覽器設置)。我看過其他網站這樣做,沒有被Firefox標記爲彈出窗口。我的鏈接被標記爲彈出窗口。也許這是因爲域名必須保持不變(這不是我的情況)? 有沒有辦法阻止我的鏈接被標記爲彈出窗口,看到新的標籤事件是由鼠標點擊觸發的?javascript firefox popup
感謝
我想在用戶點擊一個鏈接時用javascript打開一個新標籤(或窗口,取決於用戶的瀏覽器設置)。我看過其他網站這樣做,沒有被Firefox標記爲彈出窗口。我的鏈接被標記爲彈出窗口。也許這是因爲域名必須保持不變(這不是我的情況)? 有沒有辦法阻止我的鏈接被標記爲彈出窗口,看到新的標籤事件是由鼠標點擊觸發的?javascript firefox popup
感謝
我可能會做 - 這取決於具體要求 - 是的,而不是設置一個onclick
處理程序中,設置一個onmouseup
處理程序和,而不是處理程序打開該鏈接,讓處理程序設置鏈接的href
- 和而不是覆蓋瀏覽器的默認行爲。 (最後一部分在行爲方面並不重要,因爲沒有主要的默認on-mouse-up行爲,但它的可讀性非常重要!)然後,在處理程序完成之後,瀏覽器就會像用戶一樣點擊一個正常的鏈接href
:假設典型的Firefox設置,左鍵單擊將在同一個選項卡中打開鏈接,中鍵單擊將在新選項卡中打開它,右鍵單擊將打開上下文菜單帶有諸如「在新窗口中打開鏈接」和「在新標籤中打開鏈接」等選項。
Yes this seems to be exactly what I want. Thanks for the idea. I'm going to try it now and report here if I face any issues. – SimonNN
Err... after some thought there is something I find obscure about your idea: if onmouseup is supposed to initialize the href, then won't it mean I have to press twice on the link to actually open it? – SimonNN
No, because the 'onmouseup' handler is called before the normal link-handling logic happens. By the time Firefox goes, "oh, hey, he just middle-clicked on a link, I should open it in a new tab", the 'href' will already have been changed. (I tested this before posting, by creating a local HTML page consisting of just 'bar'。如果你想看看它的行爲如何,而不必大量重構你的代碼,你可以嘗試相同的測試。) – ruakh
您可以使用鏈接本身的「目標」屬性:
<a target=_blank href='whatever'>Click Me!</a>
在新窗口/選項卡中打開的鏈接。
Will that always override a popup blocker (at least, within reason)? –
Well who knows what people have built into their browsers (via add-ons or whatever), but generally an explicit user action like a mouse click on a link will allow a page to do something like that. – Pointy
True enough; I don't know enough about how popup blockers work to comment, I suppose. Just that *some* sites seem to have this down (no matter the settings...). ':s' –
一個非常簡單的解決方案,如果它只是你使用它,允許在該網站彈出窗口。您可以配置火狐像這樣:http://malektips.com/firefox_0003.html
I will be the only one using my solution indeed, but the site I want to apply my solution in is not mine, and therefore I wouldn't want to allow this site to bring me popups :). But in any case popup was probably a poor choice of words. This isn't really the behaviour I want. I tried to make my needs clearer in my other comments. Basically I want to be able to either make the link a "popup" (new tab) or not, depending on the type of click I make (left or middle, ctrl click, ..., as with normal links). The difficulty here resides in the fact that it's a js function and not a normal link. – SimonNN
what site exactly? I could help more if I knew the site so I could actually test against something – LordZardeck
你可以給你的鏈接標記/代碼? –
這是window.open(url),其中url的域名與當前用戶的域名不同。 – SimonNN
如果同一個域的window.open工作,那麼爲什麼不先在同一個域中打開一個彈出窗口,然後重定向它頁面轉到新的網址。 – Birey