Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何仅在特定URL中添加javascript代码?_Javascript - Fatal编程技术网

如何仅在特定URL中添加javascript代码?

如何仅在特定URL中添加javascript代码?,javascript,Javascript,例如,我在域的每个地方都有一个div元素。我想在ost页面上删除它,但不想在主页上删除。例如,我想在www.spiele33.de上显示它,但在http://www.spiele33.de/uncategorized/moto-x3m-4-winter/ 所以,我想用javascript来实现它 <script> var currentUrl = window.location.href; if (currentUrl.indexOf('.de/') != cu

例如,我在域的每个地方都有一个div元素。我想在ost页面上删除它,但不想在主页上删除。例如,我想在
www.spiele33.de
上显示它,但在
http://www.spiele33.de/uncategorized/moto-x3m-4-winter/
所以,我想用javascript来实现它

 <script>
    var currentUrl = window.location.href;

     if (currentUrl.indexOf('.de/') != currentUrl.length  - 4) // if it is not home page
     {
      var x = document.getElementsByClassName("entry-title h2-simulate-h1");
 x.style.display= "none";

     }
    </script>

var currentUrl=window.location.href;
if(currentUrl.indexOf('.de/')!=currentUrl.length-4)//如果它不是主页
{
var x=document.getElementsByClassName(“条目标题h2-simulate-h1”);
x、 style.display=“无”;
}

但是它不起作用

location.pathname
包含域后的所有内容,因此请检查
location.pathname==='/'

location.pathname
包含域后的所有内容,因此,检查
location.pathname==='/'

这应该在服务器端完成。如果您确实需要在脚本中完成这项工作(我同意您可能不需要),请查看是否可以在主页上生成可以测试的内容,例如,在body标签上放置一个特定的类,并进行检查,而不是尝试测试URL。这应该在服务器端完成。如果您确实需要在脚本中执行此操作(我同意您可能不需要),请查看是否可以在主页上生成可以测试的内容,例如,在body标记上放置一个特定的类,并检查该类,而不是尝试测试URL。如果(location.pathname==='/'){//hide element}应该执行该技巧,则更为具体;如果(location.pathname===='/'){//hide element}应该执行该技巧,则更为具体