Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Display Something Only on Certain URLs w/ JavaScript

    Doing some forum searching, I found the following code:
    Code:
    <div id="someThing" style="display:block" >Put something here.</div>
    
    <script type="text/javascript">
    var nowUrl = location.href;
    if (nowUrl != "http://jemmakidd.com/categories.php") {
    document.getElementById("someThing").style.display="none";
    }
    </script>
    Apparently, this is for use of when you are modifying, for example, a PHP header or footer displayed on every page and you only want something to show up on particular pages.
    My issue is that I am trying to put something in the header of a Tumblr blog, and I only want it to show on the post pages.

    The Tumblr main page format is: http://*.tumblr.com/
    The Tumblr post page format is: http://*.tumblr.com/post/*

    Is there any code I can use to do this?

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    I'm pretty sure that this will work on your example http://*.tumblr.com/post/*
    Code:
    if (nowUrl.match('post')) {
     document.write("Found it!")
     }

  • #3
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm, that doesn't seem to be working.

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    That is odd... It worked when I tried it...
    Can I see the full example after you tried that?

  • #5
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Code:
    <div id="someThing" style="display:block" >Put something here.</div>
    
    <script type="text/javascript">
    // tested URL was www.mysite.com/lib/test.html
    var nowUrl = location.href;
    if (nowUrl.match('lib')) { // testing the url for a match on "lib"
     document.getElementById("someThing").innerHTML = 'Found it!';	// this worked
     }
    else{
     document.getElementById("someThing").innerHTML = 'Not There...';	
     }
    </script>

  • #7
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much! That worked this time.
    I basically don't want it to show at all on any page other than one containing "post" so removing
    Code:
    else{
     document.getElementById("someThing").innerHTML = 'Not There...';	
     }
    would work fine, right?

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    in your example, you might sub the
    document.write("Found it!")
    for
    Code:
    document.getElementById('description').innerHTML='Found it!'
    just for the sake of testing, document.write doesn't work in all situations...

  • #9
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    no, you would have
    Code:
    else{
     document.getElementById("someThing").innerHTML = '';	
     }
    to clear the element (unless you know it is already clear...)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •