开发者

How to parse the page name from a URL?

I have a URL, and I want to get the final page name out of it. For example, if my url is http://www.mysite.com/mypage.cfm, then I want to get the value mypage.cfm. I tried googling to find if there are any built-in coldfusion fu开发者_JAVA百科nctions that can help me accomplish this, but I haven't been able to find any. So, what I did at first was implement a sort of "endsWith" function (which doesn't seem to be available in coldfusion either) - something like this:

<cfif right(cgi.http_referer, len("mypage.cfm")) EQ "mypage.cfm">
... do whatever I want to do if the page is "mypage.cfm" ...

This was working well... The problem is if there's a query string appended to the url this won't work. For example, if the url is http://www.mysite.com/mypage.cfm?param=whatever, the if statement will evaluate to false.

I can probably safely accomplish what I'm trying to do by checking if the url contains the page name using FindNoCase... however, this doesn't seem so intuitive or correct. What is the best way to strip out the page name from a url in coldfusion?


No built in functions per se, but list functions should do:

<cfset pageName = listFirst(listLast(CGI.HTTP_Referer, '/'), '?')>


If you want the current page name from the URL, try this:

 ListLast(CGI.SCRIPT_NAME, "/")

That will get the full path after the domain name (including the leading slash) and then return the last item from the list using the / as the separator.


There's a UDF on CFlib that does this: http://cflib.org/udf/GetFileFromURL

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜