简体   繁体   English

如何在LIVE-SERVER上使用Javascript查找当前的html文件页面名称

[英]How can I find the current html filepage name using Javascript, on a LIVE-SERVER

I am going to deploy this page on an FTP And I need to find out how I can detect the html file currently being viewed using JavaScript. 我将在FTP上部署此页面,并且需要了解如何检测使用JavaScript当前正在查看的html文件。

If I open the html file, it works just fine with this: 如果我打开html文件,则可以正常使用:

var fileName = location.href.substring(location.href.lastIndexOf("/") +1);

But, if I open it via my localhost adress, it has a null value. 但是,如果我通过本地主机地址打开它,则它的值为空。 So I'm guessing I have to use some other method to extract the current html file name. 所以我想我必须使用其他方法来提取当前的html文件名。 Or is there a better approach to this? 还是有更好的方法呢?

Note: I am not going to use JQuery or anything like that. 注意:我不会使用JQuery或类似的东西。 EDIT: I can get the filename if it isn't my index file.. If it's the index file I get nothing using the above code. 编辑:如果不是我的索引文件,我可以得到文件名。如果是索引文件,则使用上面的代码我什么也没得到。 Most likely since all I have in my adress bar is the localhost adress of the live-server? 由于我地址栏中的所有内容很可能是实时服务器的localhost地址?

The web deals in URLs, not file names. 网络处理的是URL,而不是文件名。

Sometimes a URL will include something that looks like a file name, and sometimes that even maps on to a real file name on the server's hard disk. 有时,URL会包含类似于文件名的内容,有时甚至会映射到服务器硬盘上的真实文件名。

When you type http://example.com/ then it might map that onto a file called index.html . 当您输入http://example.com/它可能会将其映射到名为index.html的文件中。 Or maybe on to index.php . 或者也许进入index.php Or maybe it won't touch any file but will just use logic built into the web server application to determine what to respond with. 也许它不会触摸任何文件,而只会使用Web服务器应用程序中内置的逻辑来确定响应的内容。

There's no way to know in the general case. 在一般情况下,没有办法知道。

If your specific case, you know that the path / maps onto index.html , so you can write an explicit mapping in your JavaScript code. 如果您是特定情况,则知道路径/映射到index.html ,因此可以在JavaScript代码中编写一个显式映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在运行实时服务器的情况下在 Visual Studio Code 中调试 JavaScript - How to Debug JavaScript in Visual Studio Code with live-server Running 如何关闭 Windows 上的 node.js 实时服务器? - How do I close a node.js live-server on Windows? 如何使用Sublime Text 3从Windows机器中杀死Live Server - How to kill the live-server from windows machine using sublime text 3 如何在 VSCode chrome live-server 中运行 npm 模块(puppeteer)? - How to run npm module (puppeteer) in a VSCode chrome live-server? 如何使用javascript在JSON中找到第二个密钥名称 - How can I find second key name in JSON using javascript vscode live-server extension v5.6.1 无法解析 'axios' JavaScript 导入 - vscode live-server extension v5.6.1 fails to resolve 'axios' JavaScript import 如何使用 Javascript 在字符串中找到类似 HTML 的标签? - How can I find HTML like tags in a string using Javascript? 如何找到 JavaScript 函数的名称 - How can I find the name of a JavaScript function 使用 npm 安装 live-server 包时出错。 `npm audit` 无助于解决问题 - Getting error while installing live-server package using npm. `npm audit` is not helping to fix the problem 如何在 Javascript 中获取当前目录名称? - How can I get the current directory name in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM