最后一段网址 [英] Last segment of URL

查看:86
本文介绍了最后一段网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取网址的最后一段?我有以下脚本显示点击的锚标记的完整网址:

How do I get the last segment of a url? I have the following script which displays the full url of the anchor tag clicked:

$(".tag_name_goes_here").live('click', function(event)
{
    event.preventDefault();  
    alert($(this).attr("href"));
});

如果网址是

http://mywebsite/folder/file

我怎么才能得到它在警告框中显示网址的文件部分?

how do I only get it to display the "file" part of the url in the alert box?

推荐答案

您还可以使用 lastIndexOf()函数用于查找 / <的最后一次出现/ code>您网址中的字符,然后 substr()函数从该位置开始返回子字符串:

You can also use the lastIndexOf() function to locate the last occurrence of the / character in your URL, then the substr() function to return the substring starting from that location:

window.alert(this.href.substr(this.href.lastIndexOf('/') + 1));

这样,您就可以避免创建包含所有网址的数组,如 split()确实。

That way, you'll avoid creating an array containing all your URL segments, as split() does.

这篇关于最后一段网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆