加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

js获取字符串中最后一个斜杠前面/后面的内容

(2022-04-01 16:26:02)
标签:

javascript

分类: WEB开发

参考资料如下:

js 获取字符串中最后一个斜杠后面的内容:

方法一:

var str = "https://juejin.im/test2/ferenceRoom"; 
var index = str .lastIndexOf("\/");
console.log(str .substring(index + 1, str .length)) 

方法二:

var str = "https://juejin.im/test2/ferenceRoom"; 
console.log(str.split("/test2/")[1]) 

js 获取字符串中最后一个斜杠前面的内容:

var str = "https://juejin.im/test2/ferenceRoom";
var index = str .lastIndexOf("\/"); 
 console.log(str .substring(0, index+1))

作者:那抹蓝亦如初见
链接:https://juejin.cn/post/6844903904283459597
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


js获取当前页面所在目录路径


function GetUrlPath(){
    var url = document.location.toString();
    if(url.indexOf("/") != -1){
url = url.substring(0,  url.lastIndexOf("/")) ;
    }
    return url;
  }


————————————————
版权声明:本文为CSDN博主「tyasdxx」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/macwhirr123/article/details/54708715




自己验证:

下面这个js函数已验证过可以成功取得最后一个“/"前面部分URL。


<</span>script>

function GetUrlPath(){
var url = document.location.toString();
if(url.indexOf("/") != -1){
url = url.substring(0, url.lastIndexOf("/")) ;
}
return url;
}


function delete_college(id) {
var urlPath1= GetUrlPath();
layer.open({
content: '是否确认删除'
,btn: ['删除', '取消']
,yes: function(index, layero){
// window.location.href = 'http://localhost/PHPProjectSample/Student-System/php/operation.php?operation=deletecollege&id=' + id;
window.location.href =urlPath1+'/php/operation.php?operation=deletecollege&id=' + id;
}
});
}

function modify_college(id){
var urlPath1= GetUrlPath();
var Fullpath= urlPath1+'/php/modifycollege.php?operation=modify&id=' + id;
// window.location.href = 'http://localhost/PHPProjectSample/Student-System/php/modifycollege.php?operation=modify&id=' + id;
window.location.href = Fullpath;
}

踩过坑如下:
错误:
var Fullpath= urlPath1.'/php/modifycollege.php?operation=modify&id=' + id;
正确:
var Fullpath= urlPath1+'/php/modifycollege.php?operation=modify&id=' + id;
javascript 中联结字符串不能用“."符号,(PHP中是用 “."),要用”+“符号来进行字符联结。。。
这个坑花了好多时间,真无语,一直感觉都对了就是出不来。
现在回想起来基础真的很重要。。。。


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有