使用 JavaScript 获取当前页面

Avatar of Chris Coyier
Chris Coyier

这类似于使用 JavaScript 替代 PHP 的 SCRIPT_NAME。

location.href.split('/').pop();

例如,对于以下 URL:
https://css-tricks.cn/examples/ScriptName/index.php

这段代码:

document.write( location.href.split('/').pop() );

将在页面中写入:“index.php”。