国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

利用a標簽自動解析URL分析網(wǎng)址實例

2024-05-06 16:09:57
字體:
供稿:網(wǎng)友
a標簽也和window.location一樣,也有這樣屬性,因此可以利用它來分析網(wǎng)址,下面的實例代碼,大家可以看看
 
 

對于window.location,我們比較熟悉,它有protocol,hostname,host,port,search,hash,href,pathname等屬性,a標簽也和window.location一樣,也有這樣屬性,這樣可以方便我們分析網(wǎng)址,閑話少說,上代碼



  1. function parseURL(url) { 
  2. var a = document.createElement('a'); 
  3. a.href = url; 
  4. return { 
  5. source: url, 
  6. protocol: a.protocol.replace(':',''), 
  7. host: a.hostname, 
  8. port: a.port||'80'
  9. query: a.search, 
  10. params: (function(){ 
  11. var ret = {}, 
  12. seg = a.search.replace(/^/?/,'').split('&'), 
  13. len = seg.length, i = 0, s; 
  14. for (;i<len;i++) { 
  15. if (!seg[i]) { continue; } 
  16. s = seg[i].split('='); 
  17. ret[s[0]] = s[1]; 
  18. return ret; 
  19. })(), 
  20. file: (a.pathname.match(///([^//?#]+)$/i) || [,''])[1], 
  21. hash: a.hash.replace('#',''), 
  22. path: a.pathname.replace(/^([^//])/,'/$1'), 
  23. relative: (a.href.match(/tps?:////[^//]+(.+)/) || [,''])[1], 
  24. segments: a.pathname.replace(/^///,'').split('/') 
  25. }; 
 

測試地址

console.log(parseURL("http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2"));

結(jié)果如下

 

復制代碼代碼如下:

{
file: "dom_obj_anchor.asp",
hash: "id2",
host: "www.w3school.com.cn",
params: {type: "2"},
path: "/jsref/dom_obj_anchor.asp",
port: "80",
protocol: "http",
query: "?type=2",
relative: "/jsref/dom_obj_anchor.asp?type=2#id2",
segments: [0: "jsref",1: "dom_obj_anchor.asp"],
source: http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2
}
 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 延吉市| 永善县| 安康市| 大厂| 禄丰县| 茂名市| 宁河县| 临沧市| 喜德县| 同心县| 晋城| 张北县| 密云县| 凉城县| 平果县| 淮滨县| 内黄县| 汉寿县| 新巴尔虎左旗| 湄潭县| 随州市| 汉阴县| 扶余县| 永丰县| 庆城县| 平泉县| 宜兰市| 正蓝旗| 佛学| 建昌县| 永城市| 辽源市| 大化| 巴南区| 洪泽县| 渭南市| 赤峰市| 辽宁省| 科技| 贵德县| 芒康县|