2011-10-23 12 views
0

ユーザーがリンクをクリックしたときに、新しいタブ(またはブラウザの設定に応じてウィンドウ)をjavascriptで開きたいとします。私はそれを行う他のサイトを見て、Firefoxのポップアップとして表示されません。私のリンクはポップアップとして表示されます。おそらくそれは、ドメインが同じ(私の場合ではない)のままである必要があるからでしょうか? 新しいタブイベントがマウスクリックによってトリガーされるのを見て、自分のリンクがポップアップとしてフラグが立てられないようにする方法はありますか?新しいウィンドウ/タブでリンクを開くことがjavascript firefox popup

<a target=_blank href='whatever'>Click Me!</a> 

おかげ

+0

あなたのリンクのマークアップ/コードを教えてもらえますか? –

+0

これは、window.open(url)です。ここで、urlは現在のドメイン名とは異なるドメイン名を持っています。 – SimonNN

+0

同じドメインwindow.openが動作する場合は、最初に同じドメインでポップアップを開いてから新しいURLにページを移動します。 – Birey

答えて

0

正確な要件に応じて、onclickハンドラを設定する代わりに、onmouseupハンドラを設定し、ハンドラにリンクを開く代わりに、ハンドラがリンクのhref - およびを設定していない場合は、はブラウザのデフォルトの動作を上書きします。 (その最後の部分は実際には重要な振る舞いではありません。なぜなら、マウスオーバーの振る舞いには大きなデフォルトはないからですが、読みやすくすることが重要です!)そして、ハンドラが終了すると、ブラウザはまるでユーザがその普通のリンクをクリックするとhref:典型的なFirefoxの設定を仮定して、左クリックすると同じタブにリンクが開き、中クリックが新しいタブで開き、右クリックするとコンテキストメニューが開きます「新しいウィンドウでリンクを開く」や「新しいタブでリンクを開く」などのオプションがあります。

+0

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

+0

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

+0

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

1

あなたは、リンク自体の「ターゲット」属性を使用することができます。

+0

Will that always override a popup blocker (at least, within reason)? –

+0

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

+0

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' –

0

本当に単純な解決策であれば、そのサイトでポップアップを許可するだけです。 http://malektips.com/firefox_0003.html

+0

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

+0

what site exactly? I could help more if I knew the site so I could actually test against something – LordZardeck