基于多个URL隐藏/显示内容


hide / show content based on MULTIPLE URLs

想知道是否有可能基于多个URL隐藏或显示内容?

例如。用户在家中或使用www.url.com/或www.url.com.index.html 进行索引

特定元素display: blockdisplay: none

  if(location=="http://domain.com/") {

工作得很好,但我怎么能指定多个相同或相似格式的URL呢?

if(location=="http://google.com") {
     //Do something here   
} else {
     //Do something else here   
}

您可以使用sammy.js(http://sammyjs.org/)。它有一个不错的URL路由功能。

您可以使用PHP来$_GET URL信息。假设您有以下URL:

https://www.wherever.com/some_file.php?css1=1&css2=1

在另一个时间,假设URL看起来像:

https://www.wherever.com/some_file.php?css3=1&css4=1

当您编写生成HTML:的PHP页面时

<?php
$css = '';
function link_builder(){
  $a = func_get_args(); $lo = "<link rel='stylesheet' type='text/css' href='"; $lc = "' />"; $o = '';
  foreach($a as $v){
    $o .= "$lo$v$lc";
  }
  return $o;
}
if(isset($_GET['css1'], $_GET['css2']) && $_GET['css1'] === '1' && $_GET['css2'] === '1'){
  $css = link_builder('cssPath1.css', 'cssPath2.css');
}
if(isset($_GET['css3'], $_GET['css4']) && $_GET['css3'] === '1' && $_GET['css4'] === '1'){
  $css = link_builder('cssPath3.css', 'cssPath4.css');
}
echo $css;
?>

使用这种方法,您可以提前创建单独的外部CSS文件,这些文件可以缓存到客户端的浏览器内存中。JavaScript样式无法做到这一点。

通过这样测试location.pathname

document.getElementById('specific').style.display = location.pathname === "/index.html" ? 'none' : 'block';