[英]How to check which HTML is being used from JavaScript file?
I am using one JavaScript file for multiple HTML files, but I don't want a certain part of the JavaScript file to be used for one of the HTML files. 我将一个JavaScript文件用于多个HTML文件,但我不希望JavaScript文件的某一部分用于其中一个HTML文件。 How do I check which HTML file is being used?
如何检查正在使用哪个HTML文件?
您可以使用window.location获取url并解析结果并处理这种情况。
You can use window.location
to get the current URL. 您可以使用
window.location
获取当前URL。
But, really, I think a better approach would be to have some argument or setting to activate or disable functionality. 但是,实际上,我认为更好的方法是使用一些参数或设置来激活或禁用功能。 That would give you more flexibility in the long run.
从长远来看,这将为您提供更大的灵活性。
One easy way is to pass parameters to the javascript. 一种简单的方法是将参数传递给javascript。 Look at this answer
看这个答案
you can also use globals, but parameters are cleaner and safer. 您也可以使用全局变量,但参数更干净更安全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.