Skip to content
WJunction - Webmaster Forum

[JS] indexof prob

Status
Not open for further replies.
Hi !
I have the following function :
Code:
<script>function hide(){
var str=document.location.href;
if(str.indexOf(\"?to=\"))
{document.getElementById('vip').style.display='none';}
}</script>

I am trying to hide an object if in the url contains '?to=' .
But this one is hiding it always.
Any help ?
 

3 comments

This is a small code that I use in my templates to show/hide something.

Save this file as "slide.js"
Code:
//////////////////////////////////////////////////////////////////////////////
// slide.js Example
//////////////////////////////////////////////////////////////////////////////

var Hide = "";
var varHt = 0;
var Ht = "";
var x = 0;
var y = 10;
var z = 4;
var foo = new Array();
var Speed = "";

function setup() {
    foo = document.getElementsByTagName("div");
    for (i=0;i<foo.length;i++) {
        if (foo[i].className == "hidden") {
            Hide = foo[i].id;
        }
    }
    Ht = document.getElementById(Hide).offsetHeight;
    Speed = Hide.substring(Hide.lastIndexOf('-')+1);
    document.getElementById(Hide).style.height = '0px';
    document.getElementById('toggle').style.display = 'inline';
    
    if (Speed == 1) { y = 100; z = 1; }
    if (Speed == 2) { y = 70; z = 1; }
    if (Speed == 3) { y = 40; z = 1; }
    if (Speed == 4) { y = 20; z = 1; }
    if (Speed == 5) { y = 10; z = 1; }
    if (Speed == 6) { y = 10; z = 2; }
    if (Speed == 7) { y = 10; z = 4; }
    if (Speed == 8) { y = 10; z = 7; }
    if (Speed == 9) { y = 10; z = 10; }
}

function toggle() {
    if (x === 0) {
        document.getElementById(Hide).style.height = varHt+'px';
        if (((Ht-varHt) < z) && (varHt !== Ht)) {
            varHt = Ht;
        } else {
            varHt = varHt+z;
        }
        if (varHt <= Ht) {
            setTimeout('toggle()',y);
        }
        if (varHt > Ht) {
            varHt = Ht;
            x = 1;
        }
    } else {
        document.getElementById(Hide).style.height = varHt+'px';
        varHt = varHt-z;
        if ((Ht-varHt) <= Ht) {
            setTimeout('toggle()',y);
        }
        if ((Ht-varHt) > Ht) {
            varHt = 0;
            document.getElementById(Hide).style.height = varHt+'px';
            x = 0;
        }
    }
}
Paste this in your page <head> tag:
Code:
<script type="text/javascript" src="menu.js"></script>
in same page use this where you want the "hidden" shizzle:
Code:
<a href="#" id="toggle" onclick="toggle();"><strong>Show/Hide</strong></a>
<div id="hidden" class="hidden">
echo "text here, this will be hidden, after you click Show/Hide it will show up."
</div>
:D
 
Code:
function _$(id){return document.getElementById(id) || undefined;}

var a_uri = document.location.href || '';
if(a_uri.indexOf("?to="))
{
    elem = _$('vip');
    if(elem != undefined)
    {
        elem.style.display = 'none'
    }
}

Why are you using escape chars for the quotes ?
 
Thanks litewarez i already solved it with
Code:
<script type=text/javascript>function hide(){if (document.location.href.indexOf('?to=') >= 0){document.getElementById('vip').style.display='none';}}hide();</script>

Btw the slide.js you gave is really interesting thanks !
 
Status
Not open for further replies.

About the author

Porsche_maniak
Active Member · Joined
Auto racing,Porsche,Web Development
283
Messages
0
Reactions
16
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom