通过特定URL访问我的网站时隐藏div或类

时间:2016-01-11 09:33:16

标签: javascript html url show-hide shopify

我没有使用托管服务,我的网站完全托管在shopify平台上。

我希望通过域名转发/屏蔽链接http://fetchy.ca访问隐藏 class="site-header",但显示/显示通过http://wetpaintrecords.com访问网站时。

http://fectchy.ca包含一个框架集/框架,其中隐藏了该元素。

3 个答案:

答案 0 :(得分:0)

尝试使用此代码,但我不知道这是否是解决此类问题的最佳方式。

<script>
    var url = window.location;
    if(url.toString().indexOf('fetchy.ca') != -1) {
        $('.site-class').hide();
    } else {
        $('.site-class').show();
    }
</script>

尝试一下,首先测试该位置,然后检查网址是否包含“域名”,然后根据需要隐藏“div”,但注意:当浏览器因某种原因禁用了javascript时,它不会起作用......

如果你有一个像php这样的后端,我认为这样会更容易。

答案 1 :(得分:0)

您可以使用window.location.hostname

在下面的例子中,我隐藏了一个div,如果它是JSFiddle中的访问则显示另一个。

JSFiddle

(function init() {
  if (window.location.hostname === "fiddle.jshell.net") {
    $(".header").hide();
    $(".header_2").show();
  } else {
    $(".header").show();
    $(".header_2").hide();
  }
})()
.header {
  width: auto;
  height: 50px;
  background: #ddd;
}
.header_2 {
  width: auto;
  height: 50px;
  background: #ddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<div class="header">Header 1. This is a generic header, shown for all cases.</div>
<div class="header_2">Header 2. Only shown if accessed from JSFiddle</div>

您在window.location.hostname中获得了适当的价值。

enter image description here

答案 2 :(得分:0)

尝试购买 theme.liquid index.liquid 或任何其他任何液体{{{ 1}}

class="site-header"

或者在theme.liquid文件中的任何位置添加它

{% if shop.url == 'http://wetpaintrecords.com' or shop.url == 'wetpaintrecords.com' %}
<div ... class="site-header">
.....
</div>
{% endif %}

编辑:OP正在使用iframe / frameset / frame,因此采用以下解决方案。

这里有两个选择。

选项1(最简单) - 在主题文件中添加以下内容:

{% if shop.url == 'http://fetchy.ca' or shop.url == 'fetchy.ca' %}
<style>
  .site-header{display:none !important;}
</style>
{% endif %}

选项2(不是最简单但最安全) - 找到包含&#39; 标题类=&#34; site-header&#34;的液体文件角色=&#34;旗帜&#34; &#39;并将该部分更改为以下内容:

{% unless collection %}
<style>
   .site-header{display:none !important;}
</style>
{% endunless %}

这样,只要页面是一个集合(艺术家页面按预期),该部分就不会显示。