原生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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM