国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表

圖片精選

主站蜘蛛池模板: 通化县| 兴宁市| 洪江市| 潞西市| 榕江县| 西藏| 慈利县| 洪洞县| 扎赉特旗| 临桂县| 贺兰县| 邻水| 谷城县| 普陀区| 阿瓦提县| 闵行区| 芷江| 阳谷县| 疏勒县| 榆树市| 治县。| 涟水县| 阿城市| 石门县| 峨山| 鸡西市| 泰和县| 婺源县| 三门县| 林甸县| 桃园县| 若羌县| 普宁市| 阿巴嘎旗| 西昌市| 平原县| 临江市| 同仁县| 峨边| 北海市| 监利县|