外部 javascript ファイルに SRCからURLクエリパラメーター(引数)を 渡す

Quick Edit Pencil

クライアント側(javascript) で<script>要素のsrc属性を解析する

ロード直後、自己の<script>要素のsrc属性から、URLクエリパラメータ(引数)を取得する

<HTML>
 <head>
  <script>
   src取得(../../script.js?c=book&w=dictionary)
    URLクエリパラメータ取得(c=book&w=dictionary)

// ../../script.js?c=book&w=dictionary

var o = document.getElementsByTagName("script");

var q = o[o.length-1].src.substring(o[o.length-1].src.lastIndexOf("?")+1); 

document.write(q); // c=book&w=dictionary

document.write(getUrlqp(q,"c")); // book

document.write(getUrlqp(q,"w")); // dictionary

function getUrlqp(q,p){
    return q.replace(RegExp("(^.*?)("+p+")=(.*?)(&.*?$|$)"),"$3");
}

このブログの人気の投稿

PowerShellのGetDetailsOf メソッドでプロパティの詳細情報のID番号と項目名を列挙します

AppleScript 改行 コード 置換

https://gemini.google.com/search?q=%5B%5BOralHistory%5D%5D

blender4.1 レンダリング画像を左右反転する方法

書字方向 横書方向変換スクリプト 左書きから右書きへ(コピペ用途)

ヤバい!JAPANNEXT WQHD対応 27型ワイド液晶ディスプレイ「JN-IPS2716WQHD」を注文してみた!!!