最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to conditionally display HTML based on the page URL - Stack Overflow

programmeradmin5浏览0评论

I have a few different web pages -

www.foo/index.html
www.foo/dog.html
www.foo/cat.html

Assume the pages are very similar, except for an image and some text in the middle of the page that's unique to that page.

I have an external script that dynamically creates the HTML for each page using templates and substituting in various variables and configurations. I'm trying to figure out how to display a certain piece of HTML only for a certain page (e.g. cat.html)

In psuedo-code, this is what I'd like to do -

  <style>
    function isCatPage() {
      if page.url.contains("cat.html")
        return true;
      else
        return false;
      end
    }
  </style>

  if isCatPage {
  <bold>This text only appears on cat.html</bold>
  }

  <p> This is random dynamically generated HTML</p>
</body>

Using basic javascript to show <bold> on an specific HTML page.

Thanks!

I have a few different web pages -

www.foo./index.html
www.foo./dog.html
www.foo./cat.html

Assume the pages are very similar, except for an image and some text in the middle of the page that's unique to that page.

I have an external script that dynamically creates the HTML for each page using templates and substituting in various variables and configurations. I'm trying to figure out how to display a certain piece of HTML only for a certain page (e.g. cat.html)

In psuedo-code, this is what I'd like to do -

  <style>
    function isCatPage() {
      if page.url.contains("cat.html")
        return true;
      else
        return false;
      end
    }
  </style>

  if isCatPage {
  <bold>This text only appears on cat.html</bold>
  }

  <p> This is random dynamically generated HTML</p>
</body>

Using basic javascript to show <bold> on an specific HTML page.

Thanks!

Share Improve this question edited Aug 21, 2015 at 12:28 Eric Bishard 5,3717 gold badges53 silver badges76 bronze badges asked Jun 6, 2014 at 8:41 user2490003user2490003 11.9k21 gold badges100 silver badges172 bronze badges 2
  • api.jquery./hide – Joseph Commented Jun 6, 2014 at 8:43
  • for the SEO reasons, I would do this server side and not JavaScript. – Dexa Commented Jun 6, 2014 at 8:46
Add a ment  | 

2 Answers 2

Reset to default 3

I would use jquery and do something like the following:

<script type='text/javascript'>
    // only run this when the page finishes loading
    $(document).ready(function () {
        // if cat.html exists in the url
        if (window.location.href.indexOf('cat.html') > -1) {
            // select the p by its id and hide it or - 
            $('#conditional').css('visibility', 'visible');
        }
        else {
            // show it
            $('#conditional').css('visibility', 'hidden');
        }
    });
</script>

<p id='conditional'>This text only appears on cat.html</p>
<p>This is random dynamically generated HTML</p>

Get the current url, and split it

var url_parts = location.href.split(/\//g);
var page = url_parts[url_parts.length-1];

page should contain the current page (eg cat.html)

Although, I'd really suggest you use a server for this kind of stuff

发布评论

评论列表(0)

  1. 暂无评论