这是我的第一篇文章。我对 javascript 相当陌生,并试图使用以下函数来获取文档名称(由页面上的元素 id 模仿)以在以后的函数中使用。在测试站点上,它运行良好。例如,如果文件是http://testserver/options/example.html
它返回“示例”
一个现场站点,它总是返回www
<script type="text/javascript">
$(document).ready(function() {
var pageName = function() {
//this gets the full url
var url = document.location.href;
//this removes the anchor at the end, if there is one
url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
//this removes the query after the file name, if there is one
url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?")); console.log(url);
//this removes the file extension, if there is one
url = url.substring(0, (url.indexOf(".") == -1) ? url.length : url.indexOf(".")); console.log(url);
//this removes everything before the last slash in the path
url = url.substring(url.lastIndexOf("/") + 1, url.length); console.log(url);
//return
return url; console.log(url);
}
});
</script>