Class OpenInNewTabAction
- All Implemented Interfaces:
Serializable
window.open(url,
"_blank", features) when the bound trigger fires.
Most browsers block window.open calls that don't happen inside a
transient user activation. Bind this action to a Trigger that fires
during such a gesture — typically a ClickTrigger — so the call
inherits the gesture and the popup is allowed.
Two axes of configuration:
- URL — either a known
Stringor anAction.InputofStringresolved on the client at fire time (e.g. the current value of an input field, or ablob:URL the client just minted). - Features — the third argument to
window.open, controlling popup characteristics such as opener access (the standard"noopener"/"noreferrer"tokens) and window sizing ("width=600,height=400"). When not supplied, defaults to "noopener,noreferrer", which severswindow.openeron the new page and strips theRefererheader — the safe default for opening untrusted URLs. Callers that supply their own features are responsible for including those tokens themselves if they want the same protection.
The target is always "_blank" — opening into the current tab is not
the point of this action, and reusing a named window has different popup
blocker semantics that this primitive does not try to model. If you need to
navigate the current tab, use Page.setLocation; if you need named
windows, call window.open via executeJs directly.
javascript: URLs are rejected: static-String URLs throw
IllegalArgumentException from the constructor, and Action.Input URLs
resolved on the client are silently dropped at fire time (the generated JS
short-circuits window.open). This prevents an attacker-controlled
value flowing through a PropertyInput from executing arbitrary script
in the opener's origin. The check mirrors the URL parser's leading-whitespace
handling (C0 controls and ASCII space are stripped before the scheme) so a
leading tab/newline can't smuggle one through.
No outcome callback: the browser does not reliably report whether the popup
was blocked (the spec lets window.open return null, but not
every blocker path does so), and never reports when the new tab is closed.
// Open a known URL in a new tab.
new ClickTrigger(button)
.triggers(new OpenInNewTabAction("https://vaadin.com/docs"));
// Open whatever URL the user typed into a field.
Action.Input<String> urlInput = new PropertyInput<>(urlField, "value",
String.class);
new ClickTrigger(button).triggers(new OpenInNewTabAction(urlInput));
// Open a sized popup window with custom features. Note that supplying
// features replaces the noopener/noreferrer defaults — include them
// explicitly if you want the same protection.
new ClickTrigger(button).triggers(new OpenInNewTabAction("/help",
"noopener,noreferrer,width=600,height=400"));
For internal use only. May be renamed or removed in a future release.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.component.trigger.internal.Action
Action.Input<T> -
Constructor Summary
ConstructorsConstructorDescriptionOpens a URL resolved on the client at fire time, with the default features ("noopener,noreferrer").OpenInNewTabAction(Action.Input<String> url, Action.Input<String> features) Opens a URL resolved on the client at fire time, with features also resolved on the client.OpenInNewTabAction(String url) Opensurlin a new tab with the default features ("noopener,noreferrer").OpenInNewTabAction(String url, String features) Opensurlin a new tab with the givenfeatures. -
Method Summary
Modifier and TypeMethodDescriptionprotected JsFunctionBuilds theJsFunctionthat runs this action when the surrounding trigger fires.Methods inherited from class com.vaadin.flow.component.trigger.internal.Action
warnIfNotVisible
-
Constructor Details
-
OpenInNewTabAction
Opensurlin a new tab with the default features ("noopener,noreferrer").- Parameters:
url- the URL to open, notnulland not ajavascript:URL- Throws:
IllegalArgumentException- ifurlstarts withjavascript:(after stripping leading whitespace/control characters)
-
OpenInNewTabAction
Opensurlin a new tab with the givenfeatures. The features string is passed verbatim as the third argument towindow.open; supplying it replaces — does not extend — the default features, so include"noopener,noreferrer"explicitly if you want the same protection.- Parameters:
url- the URL to open, notnulland not ajavascript:URLfeatures- the features string forwindow.open, notnull- Throws:
IllegalArgumentException- ifurlstarts withjavascript:(after stripping leading whitespace/control characters)
-
OpenInNewTabAction
Opens a URL resolved on the client at fire time, with the default features ("noopener,noreferrer").javascript:URLs produced by the input are blocked on the client.- Parameters:
url- input supplying the URL when the trigger fires, notnull
-
OpenInNewTabAction
Opens a URL resolved on the client at fire time, with features also resolved on the client.javascript:URLs produced by the input are blocked on the client.- Parameters:
url- input supplying the URL when the trigger fires, notnullfeatures- input supplying the features string when the trigger fires, notnull
-
-
Method Details
-
toJs
Description copied from class:ActionBuilds theJsFunctionthat runs this action when the surrounding trigger fires. The returned function takes one runtime argument namedevent(declared by the framework when it composes the trigger handler); subclasses do not declare argument names themselves.The body is one statement. To embed a value produced on the client, capture an
Action.Input'sJsFunctionas a capture and invoke it inside the body as$N(event).
-