Handling Devices Capable of Sharing Screens and Devices Sharing via Copying: Web Share API

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About This ArticleThis article was generated using an automated generation workflow powered by generative AI. We cross-referenced the W3C Web Share API recommendation, MDN technical documentation, and fixed code from existing learning materials, and simulated sharing operation branches in Node.js.

Verification Status: 📘 Official information confirmed and branch logic reference tested. Actual browser hardware and OS sharing operations are unconfirmed.

When you want to share a webpage's information with an acquaintance, it is convenient to use the device's share destination selection screen. On the other hand, opening the same page on a computer may sometimes disable the share button. Even without a share function, allowing users to select and manually copy the URL helps them achieve their goal. This material compares share destination selection, cancellation, and fallbacks for unsupported environments.

Open Live Demo

Operate First

Pressing the execute button creates a read-only text field displaying the current page's URL and a share button. Verify that the displayed URL belongs to this published educational material before pressing the button. If the share destination selection screen appears, you do not need to select an actual recipient to send it. Choose cancel to return to the screen. Check whether a cancellation message appears in the result field.

In some cases, the selection screen does not appear, and the URL is simply selected. In that case, copy it manually and paste it into an empty notepad or similar tool to verify that it matches the original URL. The demo does not automatically overwrite the clipboard. On a computer, copy the selected text string using Ctrl+C or a similar shortcut, and on a smartphone, use the selection menu. Since operation methods vary by device, re-select the URL field manually as needed.

Observable StatesExpected Display in the Result Field
Sharing Request AcceptedPassed to the device sharing process.
AbortError ReturnedSharing was cancelled.
Not Supported or Other ExceptionPlease copy the selected URL.

An AbortError can be returned not only when the user cancels the operation, but also when no share target is available. Because the educational material groups these into the same message, do not uniquely determine the cause based solely on the result field. Check on the actual device to see which branch is executed.

Workflow of Sharing Function and Fallback Operations

flowchart TD
    A["共有するボタン"] --> B{"shareを利用できる?"}
    B -- "はい" --> C["共有処理を呼び出す"]
    B -- "いいえ" --> F["URLを選択して手動コピーを案内"]
    C --> D{"処理結果"}
    D -- "解決" --> E["端末へ渡した旨を表示"]
    D -- "AbortError" --> G["取り消しを表示"]
    D -- "その他の例外" --> F

The diagram illustrates the process branching based on the share button. If the sharing process is unavailable, it proceeds to the fallback path of selecting the URL. Even if the API exists, actual calls may be rejected, so the same fallback is provided for exception handling as well. However, another transmission is not automatically executed for users who have cancelled. Respect the user's decision to finish the operation.

Values to Share and Processing Mechanism

The implementation retrieves the current page URL from location.href and places it into a read-only textarea. Within the button click event, it checks for the existence of navigator.share, and if available, passes the page title and URL. This code does not include file selection and transmission, and is limited to URL sharing.

try {
  if (!navigator.share) throw Error();
  await navigator.share({ title: document.title, url: u });
  say('端末の共有処理に渡しました。');
} catch (e) {
  if (e.name === 'AbortError') {
    say('共有を取り消しました。');
  } else {
    p.select();
    say('選択したURLをコピーしてください。');
  }
}

This extracts the main points of the static educational material. Here, u is the currently displayed URL, p is the input field for copying, and say is a common function that outputs text to the results field. This is not a complete HTML file that can be pasted and run independently. For code including the execution environment, refer to the GitHub sample at the end.

Results Differ by Device Even with the Same Code

The list of share targets cannot be freely determined on the page side. It varies depending on browser and OS features, installed share targets, and other factors. The W3C specification defines it as an interface that passes data to a share target chosen by the user. According to MDN, share is not necessarily available in all major browsers and requires a secure context. There are also restrictions such as the web-share Permissions Policy and the requirement for a transient user activation.

Open the same URL on a smartphone and a computer, and compare the results of the share button. Record the browser name, version, OS, results field, and the presence or absence of the share dialog. Different behaviors do not necessarily mean the code is incorrect. Do not assume that a specific OS will always succeed before obtaining actual measurement results per device. If file sharing is also required, separately consider capability checks using canShare for data support. This material's exercise does not experiment with canShare or file sharing.

Completion of the Sharing Process Differs from Arrival at the Recipient

The timing when the Promise returned by share resolves varies by environment. MDN indicates that on Windows, it may resolve when the share dialog is launched, whereas on Android, it may resolve when data is passed to the share target. Neither proves that the recipient has received and opened the link. Therefore, do not display transmission complete on the screen, but instead use a message guiding that the data was handed over to the device's sharing process.

In business applications, if recipient receipt is required, design a separate confirmation method. Do not use a successful share button display alone as definitive proof of application acceptance or notification delivery. That the shared value is correct, that the recipient has viewing permissions, and that the link destination is valid are separate verification items.

Troubleshooting When Issues Occur

If the share button is not generated, verify whether the execution button was pressed and whether JavaScript loading failed. If the sharing feature is not provided, proceed to manual URL copying. If it is provided but rejected, check the execution environment such as HTTPS, the relationship with user actions, and permission settings of the embedded page in order. The demo groups exceptions other than AbortError into a single fallback path. To investigate causes in production, design mechanisms to record error names without including user information.

Allowing users to select the copy field themselves is particularly important in environments that do not support the Share API. Not only display the URL, but also communicate what the user should do through text. Test whether keyboard usage, touch interaction, and screen reading are supported on the actual target devices. If the diagram is not displayed, you can still follow the processing through the branching text above.

Secure Sharing and Business Applications

The educational material uses the current URL as-is. If the URL contains queries or fragments, they may also be included. In business systems that generate URLs for sharing, verify whether the URL contains confidential information, temporary tokens, personal information, or internal search criteria. Selecting a share target may pass information to external apps. This experiment uses the public material URL and defaults to cancellation on the share target selection screen. Do not use actual customer data or internal organizational links for testing.

For internal guide pages, it is best to design them so that URLs can be copied even without a share button, and to verify the environments of both the sender and receiver separately. There is no need to introduce paid APIs or external delivery services solely for sharing, but the terms of use and permissions of pages and share targets must be observed separately. The goal of this material is to provide both convenient device-dependent features and environment-agnostic fallback operations together.

Verification Scope

On December 1, 2026, W3C and MDN documentation were cross-referenced with the HTML of the static educational material. The share event handling in the material was executed on Node.js v22.16.0 by replacing it with test functions and input fields, confirming all four cases: normal resolution, AbortError, NotAllowedError, and no API. Normal and cancellation cases had no URL selection, while rejection and non-support cases included URL selection. This is a reference test for branching logic and does not constitute actual measurement of OS share screens, actual DOM selection, Pages operation, transmission to recipients, Mermaid rendering, or browser compatibility tables. Network environments and browser support are required for actual display.

References

GitHub sample

Implementation and experimental procedure

Document information

Article title
Handling Devices Capable of Sharing Screens and Devices Sharing via Copying: Web Share API
Published
Updated
Source
https://papanda925.com/?p=19165&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL