Local storage (1) Obtain the URL address of the page and determine whether the URL contains a specific value; sessionstorage, localStorage clear removeItem

本文介绍jquery/js获取当前页面url地址的方法,在jquery与js中获取当前页面url方法是一样的,
因为jquery没有自己相关的函数,使用js 的windows方法来获取,
相关方法如下:
window.location.pathname //设置或获取对象指定的文件名或路径
window.location.href //设置或获取整个 URL 为字符串
window.location.port //设置或获取与 URL 关联的端口号码
window.location.protocol //设置或获取 URL 的协议部分
window.location.hash //设置或获取 href 属性中在井号“#”后面的分段
window.location.host //设置或获取 location 或 URL 的 hostname 和 port 号码
window.location.hostname //设置或获取 location 或 URL 的 hostname 和 port 号码
window.location.search //设置或获取 href 属性中跟在问号后面的部分
window.location //属性 描述 hash 设置或获取 href 属性中在井号“#”后面的分段




avascript判断字符串中是否包含某字符串,js字符串中查看子字符
indexOf函数方法示例用法: 
function IndexDemo(str2){  
   var str1 = "BABEBIBOBUBABEBIBOBU"  
   var s = str1.indexOf(str2);  
   return(s);  
} 
例如博客中的快速评论代码:
var url = window.location.pathname;  
if(url.indexOf("post") >= 0 ) { //判断url对象文件名中是否包含post
  alert('这是文章页面');  
} 
其他示例:
var url = window.location.href;
if(url.indexOf("link") >= 0 ) { //判断url地址中是否包含link字符串,如果包含将改写元素标签id为nav0和nav1的class样式名称
  document.getElementById("nav0").className = "header_nav_menua";
  document.getElementById("nav1").className = "header_nav_menub";
}




if(url.indexOf("tag") >= 0 ){ //判断url地址中是否包含tag字符串,如果包含将改写元素标签id为nav0和nav1的class样式名称
  document.getElementById("nav0").className = "header_nav_menub";
  document.getElementById("nav1").className = "header_nav_menua";
} 

Project example:

data(){
       pathmenu:'',
}




//如果路由中有path=menu,就本地存储
if(window.location.search.indexOf("path=menu") >= 0){//判断传的路径中是否带参数path=menu
       window.sessionStorage.setItem('PATHMENU', window.location.search);                 
}
this.pathmenu = window.sessionStorage.getItem('PATHMENU');//使用本地存储
if(this.pathmenu != '?path=menu'){
    window.sessionStorage.removeItem('PATHMENU');//在这里要清除sessionstorage
    //do something
.......................

}else if(this.pathmenu == '?path=menu'){
 window.sessionStorage.removeItem('PATHMENU');//在这里要清除sessionstorage
     //do something
    .......................

}

session:
存储:
window.sessionStorage.setItem('key',vlaue);//key是自定义的命名,存储是的名字,value;要存储的值

取值:
window.sessionStorage.getItem('key');
赋值//可在页面中使用
this.a =  window.sessionStorage.getItem('PATHMENU');

清除:
window.sessionStorage.removeItem('key');

local:

存储:
window.localStorage.setItem('key',vlaue);//key是自定义的命名,存储是的名字,value;要存储的值

取值:
window.localStorage.getItem('key');
赋值//可在页面中使用
this.a =  window.localStorage.getItem('PATHMENU');

清除:
window.localStorage.removeItem('key');


 

Guess you like

Origin blog.csdn.net/Sunny_lxm/article/details/91040116