本篇使用 Javascript , 若想用jQuery的方法,可參考本站另一篇
本篇提供兩種方法,擇一使用
方法一:
/* VECTOR COOL https://vector.cool */ function getUrlParameter(sParam) { var sPageURL = decodeURIComponent(window.location.search.substring(1)), sURLVariables = sPageURL.split('&'), sParameterName, i; for (i = 0; i < sURLVariables.length; i++) { sParameterName = sURLVariables[i].split('='); if (sParameterName[0] === sParam) { return sParameterName[1] === undefined ? true : sParameterName[1]; } } };
方法二:
/* VECTOR COOL https://vector.cool */ function getURLParameter(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, "\\$&"); var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); }
用法:
/* VECTOR COOL https://vector.cool */ var tech = getUrlParameter('technology'); var blog = getUrlParameter('blog');