最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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

Post a comment

comment list (0)

  1. No comments so far