Sharing Invoice Filters via URL: A Live Demo of URLSearchParams

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

About this articleThis article is created using an automated generation workflow powered by generative AI. It demonstrates how to use the department and processing status stored in the URL to open a mock invoice list under the same conditions.

Verification Status: 🧪 Checked official specifications and verified the JavaScript syntax of the distributed HTML. Screen operations in the browser and behavior on internal sites have not been confirmed.

You sent an invoice list URL to a colleague saying, 'Please check only the unprocessed items for the Sales Department.' However, when they opened it, the General Affairs Department and processed items all appeared as well. This is because the browser only remembers the conditions you selected within the screen itself. What you actually want is not a copy of the list, buta clue to open the list under the same conditions. Let's embed that clue into the URL.

ItemScope of this material
TargetA JavaScript-enabled browser. The downloadable version is for restoration experiments in the same environment.
Data5 mock invoices. No actual customers, invoices, or login required.
What can be doneFiltering by department and status, creating conditional URLs, and restoring when reopened
What cannot be doneGranting access permissions to colleagues or sending the local HTML itself

First, search for 1 item using 'Sales Department – Unprocessed'

Open Live DemoThis simulates the display of five fictitious invoices. If your organization's settings prevent you from opening them, do not change the settings; instead, compare the conditions and results in the table below.

Select "Sales" for "Department" and "Pending" for "Status". The expected result is 1 item, 12,000 JPYnext, keeping the "Department" the same, change only the "Status" to "All". This time it is 2 items, 27,000 JPYBy changing only one condition, you can determine which difference caused the change in the number of items.

SelectionExpected Result
Sales + Pending1 item, 12,000 JPY
Sales + All2 items, 27,000 JPY
Accounting + Processed0 items, 0 JPY
Both set to "All"5 items, 49,500 JPY

Next, revert to Sales and Pending, and click "Create Conditional URL". Reopen the displayed link in the same browser and verify that the selection of Sales and Pending, along with the result of 1 item and 12,000 JPY, is restored. This does not transfer list rows to the URL; it uses the same two conditions when opening the screen.

flowchart LR
    A["部署と状態を選ぶ"] --> B["条件だけをURLにする"]
    B --> C["同じ画面で開く"]
    C --> D["条件を読み戻して表示を絞る"]

Keep conditions in the URL. Do not include line items or permissions.

After the ? in the URL, you can include name-value pairs.department=sales&status=open in this learning material represents the rule "department is Sales, processing status is unprocessed." The alphanumeric value is an identifier determined within the code, and the browser does not inherently know the Japanese department name. Even if the display name of the same department changes from "Sales" to "Sales Dept.," if the identifier sales remains unchanged, the old link can be reloaded. If the identifier is deprecated due to department consolidation or similar changes, the previous conditions cannot always be reproduced.

const link = new URL(location.href);
link.searchParams.set('department', 'sales');
link.searchParams.set('status', 'open');
const department = link.searchParams.get('department');

set registers names and values as pairs. If a condition with the same name is already registered, it is overwritten. If you manually concatenate characters using &, it becomes easy to confuse the & included in the value with the & used as a delimiter for the next condition. The task of converting characters to match the URL is left to URLSearchParams. Conversely, when opening the screen, the values are read, the selection fields are restored, and the list is filtered. The code snippet above focuses only on the core reading and writing. Whether the selected department is valid and whether conditions are duplicated are verified separately in the distributed HTML.

You should only write conditions to the URL that do not pose a problem if seen by others. URLs may remain in browser histories and server logs. Do not pass client names, invoice numbers, full names, passwords, or similar sensitive information using this method. Additionally, writing department=sales does not increase your privileges to view sales department information. In a production system, the server first determines the scope that users are allowed to see, and screen conditions are applied within that scope. This HTML does not include such privilege management or login functionality.

Distinguishing between 0 results and cases where conditions could not be reloaded

Next, select the Accounting Department and Processed status to create a link. If you reopen the page and the selection fields are as expected, and the list shows 0 results, you can determine that the conditions were successfully reloaded, but no matching rows were found. Even if you intended to display the Sales Department, if the selection fields revert to "All," a successful restoration cannot be claimed merely because a count is displayed.

Opened screenArea to distinguishNext verification
Sales Department, unprocessed, 1 resultSelection fields and amounts matchExpected restoration
Accounting Department, processed, 0 resultsSelection fields show Accounting Department, processedEmpty result is normal
Unknown or duplicate conditionsDisplay the reason and do not show the listReset the conditions and reselect
flowchart TD
A["URLで画面を開く"] --> B{"条件が受け付けられたか"}
B -->|いいえ| C["理由を読み条件を選び直す"]
B -->|はい| D{"結果は0件か"}
D -->|はい| E["条件に合う明細がない"]
D -->|いいえ| F["件数と合計を確認"]

If an old link contains a department that no longer exists at present, or if the "department" condition is included twice, it cannot be determined which one to display. In the training material, getAll is used to check the number of values with the same name, and if the value is unknown or duplicate, the list is not displayed and the reason is shown instead. This is because silently reverting to "All" would lead the viewer to mistakenly assume the amount is for the sales department only. Use "Reset conditions" to select again, and "Create conditional URL" to recreate the link. Even on an actual site, checking the selection fields on the screen opened by the other party beforehand can reduce confusion with inquiries about item counts and amounts.

Deciding whether to pass "conditions" or "morning results" to a colleague

If you are simply handing over work before the Monday deadline with the instruction "Check the unprocessed items for the sales department," a conditional URL is useful. This is because the other party can immediately navigate to the necessary conditions within the permitted details. On the other hand, if you want to keep a record that there was 1 unprocessed item worth 12,000 yen at 9:00 AM, a link alone is not sufficient. If that 1 item is processed in the afternoon, the result will change on the production screen even under the same conditions. When including monetary amounts in a report, record the confirmation time and save it in a location approved by the organization. Since the 5 items in this training material are fixed, this change that occurs in actual work was not reproduced in the demo.

flowchart TD
A["相手に何を渡したいか"] --> B{"後で再現したいものは何か"}
B -->|同じ条件| C["条件付きURL"]
B -->|結果を固定したい| D["時刻つきの承認済み記録"]

Before actually sending, verify whether the other party can open the same page with their own account. The URL of an HTML file downloaded to your own PC only points to the file on that PC. Even if you send that URL, the HTML itself is not delivered. For workplace collaboration with colleagues, it is a prerequisite that there is a shared page approved by the administrator and that both parties can open that page. If the other party cannot open the page or details, check availability starting from a page without conditions. Do not lend your account or broaden link settings so that anyone can open them.

flowchart TD
A["同僚が画面を開く"] --> B{"自分の権限で開けるか"}
B -->|いいえ| C["管理者に利用可否を確認"]
B -->|はい| D["部署と状態を復元"]
D --> E["相手が見てよい明細だけ確認"]

In handover notes, you can cross-check by writing "Link to select Sales Department and Unprocessed" alongside the URL. If the department heard verbally differs from the department on the opened screen, you can halt the process right there. This avoids having the other party read only the short identifiers contained within the URL.

The original training material was an example of retaining department conditions in the URL and returning to the selection field. This time, you can also return the processing status and test up to the point where the amounts and item counts change. When your item count differs from a colleague's, first check the selection conditions, then the range permitted to the user, and finally the time viewed. Comparing whether these three are identical is a safer way to investigate the cause than adding real invoice numbers or client names to the URL.

GitHub Sample

Fixed version of training material (HTML, operation procedures, verification code). If the public demo cannot be opened, you can download the HTML and practice the same exercises in your browser.

Reference Materials

Glossary

  • Query string: Conditions placed after the "?" in a URL. Allows communicating departments, processing statuses, etc.

  • URLSearchParams: A JavaScript feature for reading and writing URL parameters as name-value pairs.

  • View permission: The rule determining whether a user is allowed to view data. This cannot be altered via URL parameters.

  • Local HTML: Web page files stored on the user's device. The file location is not necessarily shared with other devices.

Document information

Article title
Sharing Invoice Filters via URL: A Live Demo of URLSearchParams
Published
Updated
Source
https://papanda925.com/?p=19223&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