在 js 中解析 url 只靠現有的 location 提供的功能,其實有點不方便,
像是要抓資料夾名稱、檔案名稱、參數值、或是 hashTag 值,都沒有。所以寫了一個 js plugin,幫忙 location 加裝屬性,方便控制。
使用方式:
先 import urlplugin.js
接下來將會新增參數在 location 上面
以下是 屬性說明
假設要解析的網址為
http://localhost:8080/aa/bb/index.html?s=1&n=2#editor/target=post;postID=585277474577843162;onPublishedMenu=overview;
plugin.js 將會解析網址並寫入 location.sec 陣列中,如果要取得 aa 那就會是 location.sec[1]
參數定義,解析網址後,寫入 location.params 物件中,如果要取得 s 那就會是 location.params.s
HashTag 定義,解析網址後,寫入 location.hashs 物件中,如果要取得 postID 那就會是 location.hashs.postID
GIT位置如下
https://github.com/LeoYeh/urlPlugin
有問題再麻煩通知,謝謝。
留言
張貼留言