2019年3月12日 星期二

[JavaScript] Hide Url Parameter

最近遇到一個需求,需要將網址的參數拿掉
找了一些作法,可能要把參數放在cookie, session
或是塞DB,再從DB取資料出來,這都還要加工處理

後來查到github上面有人分享,更改history的狀態就可以
只要在一開始呼叫這個function,便可以參數隱藏了

function getURLParameter(name) {
 return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);
}
function hideURLParams() {
 //Parameters to hide (ie ?success=value, ?error=value, etc)
 var hide = ['success','error'];
 for(var h in hide) {
  if(getURLParameter(h)) {
   history.replaceState(null, document.getElementsByTagName("title")[0].innerHTML, window.location.pathname);
  }
 }
}

Ref: https://gist.github.com/ScottKaye/5158488#file-hide-url-parameters-js

沒有留言:

張貼留言