简体   繁体   English

如何从JavaScript文件检查正在使用哪个HTML?

[英]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.

相关问题 如何使用 JavaScript 检查 Chrome 中正在使用网络摄像头 - How to check with JavaScript that webcam is being used in Chrome jQuery / Javascript:如何删除当前页面未使用的页面的JS文件 - Jquery/Javascript: How to Remove JS file for a page which is not being used in current page 如何检查集合中使用了哪个变量? - How do I check which variable in a collection is being used? 如何从未压缩的html中使用html获取原始图像数据? - How to get the raw image data from html being not compressed used javascript? 如何检查用户当前在哪个 html 文件上? - How to check which html file the user is currently on? 如何检查网络应用程序中使用了哪些图像/ css / javascript? - How to check which images/css/javascript are used in web app? 如何将输入值从HTML传递到JavaScript,以用于切换包含函数的案例 - How to pass input value from a HTML to JavaScript to be used for switching the cases which contains functions 我可以排除在页面上使用 javascript 文件吗? - Can I exclude a javascript file from being used on a page? JavaScript 以编程方式检查是否正在使用相机 - JavaScript programatically check if camera is being used 哪些javascript文件未在页面加载时使用 - which javascript files are NOT being used on page load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM