原生js添加及获取cookie&获取上一页面url
<script type="text/javascript"> function toApp(url){//主函数 var ref = ''; if(getCookie('pre_url')){ ref = getCookie('pre_url'); }else{ if (document.referrer.length > 0) { ref = document.referrer;
if (ref.length == 0 && opener.location.href.length > 0) {
ref = opener.location.href;
} }if(ref.indexOf("irootech.com") > 0 && ref.indexOf("sany") > 0){//验证是否是需要的url setCookie('pre_url',ref,1); }else{ ref = ''; } } window.location.href=ref+'#'+url;// 跳转到指定的url }
function setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires="+d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } </script>