jQuery(function ($) { $(function () { // anchorsにaタグを配列ですべて格納 const anchors = document.getElementsByTagName("a"); // 条件判定用のURLを取得 const matchStr = document.domain; for (let i = 0; i < anchors.length; i++) { // アンカータグを変数に格納 const linkElement = anchors[i]; // hrefの中身を変数にとりだす let linkUri = linkElement.getAttribute('href'); // 空じゃなければ次へ if (linkUri) { // 条件判定用のURLを含んでる、もしくはスラッシュで始まっていれば if (linkUri.match(matchStr) || linkUri.startsWith('/')) { // 末尾にスラッシュがあれば削除 linkUri = linkUri.replace(/\/$/, ''); // パラメータを付与して配列に戻す anchors[i].href = linkUri + "?from_app=true"; } } } }); });WordPress のテーマに実装する場合はこのままペタッとはればok。
目次
参考記事
cly7796.net

URLを取得したり書き換えたりするURLを取得したり、書き換えたりするサンプルです。 サンプルコード(URLを取得) JavaScript HTML 現在...
Stack Overflow

How can I add or update a query string parameter?With javascript how can I add a query string parameter to the url if not present or if it present, update the current value? I am using jquery for my client sid...
Stack Overflow

How to create conditional hyperlink in HTMLI want to link my page with another page on condition.
Suppose I have 3 HTML pages, namely 1.html, 2.html and 3.html. What I want is that if 1.html is loaded ...
Tips Note by TAM

JavaScriptでURLのパラメータやアンカーを判断して処理を変更する方法 | Tips Note by TAMTAM のテクニカルチームがお届けする WEB技術ブログ!
コメント