...

View Full Version : Display Something Only on Certain URLs w/ JavaScript



CSS
09-22-2011, 05:46 AM
Doing some forum searching, I found the following 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?

blaze4218
09-22-2011, 06:39 AM
I'm pretty sure that this will work on your example http://*.tumblr.com/post/*


if (nowUrl.match('post')) {
document.write("Found it!")
}

CSS
09-26-2011, 08:29 PM
Hmm, that doesn't seem to be working.

blaze4218
09-26-2011, 08:31 PM
That is odd... It worked when I tried it...
Can I see the full example after you tried that?

CSS
09-26-2011, 08:34 PM
Sure: http://199stalgia.tumblr.com/post/9077491964

blaze4218
09-26-2011, 08:36 PM
<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>

CSS
09-26-2011, 08:39 PM
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

else{
document.getElementById("someThing").innerHTML = 'Not There...';
}
would work fine, right?

blaze4218
09-26-2011, 08:40 PM
in your example, you might sub the
document.write("Found it!")
for


document.getElementById('description').innerHTML='Found it!'

just for the sake of testing, document.write doesn't work in all situations...

blaze4218
09-26-2011, 08:41 PM
no, you would have


else{
document.getElementById("someThing").innerHTML = '';
}
to clear the element (unless you know it is already clear...)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum