外部 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番号と項目名を列挙します

PowerShellで複数ファイルのプロパティを取得する方法(準備編)

簡単 YouTube動画をダウンロード、音声のみ保存する方法 2019

DOMノードオブジェクトを文字列に変換する

PowerShellでJPG画像のリサイズとウォーターマーク画像との合成を同時に行う

AppleScript 改行 コード 置換

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