ダイナミックブロックを利用する上でのカスタマイズ例を集めました。
※ 以下はサンプルコードです。 このままコピー&ペーストしても動作しませんのでご注意ください。
ユーザー情報変数をイベント以外からセットする
krtBlock.setVal(’変数名','値(処理内容)');
1:URLから指定したパラメータの値を取得する
// 1:検索したいクエリパラメータのkeyを指定する
const key = "rank";
function getQueryParameterValue(key) {
const params = new URLSearchParams(window.location.search);
return params.get(key);
}
// 1の値をユーザー情報変数にセットする
krtBlock.setVal('paramName', getQueryParameterValue(key));
2:URLから特定のパスの直後のパスを取得する
// 1:パスを指定
const specificPath = "path";
// 現在のページのパスを取得
const fullPath = window.location.pathname;
// 1のパスの後ろのパスを取得
let trailingPath = "";
const index = fullPath.indexOf(specificPath);
if (index !== -1) {
const trailingPath = fullPath.slice(index + specificPath.length);
const pathSegments = trailingPath.split('/').filter(segment => segment);
if (pathSegments.length > 0) {
nextPathSegment = pathSegments[0];
}
}
// パスをユーザー情報変数にセットする
krtBlock.setVal('pathName', nextPathSegment);
3:URLから指定した位置のパスを取得する
// 1:パスの位置を指定
const n = 2;
// 現在のページのパスを取得
const fullPath = window.location.pathname;
// n番目のパス要素を取得する関数
function getNthPathSegment(path, n) {
const pathSegments = path.split('/').filter(segment => segment);
return pathSegments.length >= n ? pathSegments[n - 1] : null;
}
// パスをユーザー情報変数にセットする
const nthPathSegment = getNthPathSegment(fullPath, n);
krtBlock.setVal('pathName', nthPathSegment);