...

View Full Version : quick bool check problem.



themindco
03-09-2012, 04:10 PM
I thought this would have worked but it doesn't seem to. Not very familiar with java script anymore I guess. Someone see what I am doing wrong?


<style type="text/javascript">
var contact = document.getElementById("contactPage") ? true:false;
var projects = document.getElementById("productPage") ? true:false;
var about = document.getElementById("aboutPage") ? true:false;
var home = document.getElementById("homePage") ? true:false;

var checkPage = function (contact, projects, about, home)
{
if (contact === true)
else(projects === true)
else(about === true)
else(home === false)
document.getElementById("homePage").style.display='block';
};

checkPage(contact, projects, about, home);
</style>

blaze4218
03-09-2012, 04:17 PM
What do you expect to occur?
you have 3 else statements following 1 if, I believe you require else if, and code to execut

var checkPage = function (contact, projects, about, home)
{
if (contact === true){}
else if(projects === true){}
else if(about === true){}
else(home === false) {
document.getElementById("homePage").style.display='block';
}
};

blaze4218
03-09-2012, 04:19 PM
this line tests whether the element exists.

var contact = document.getElementById("contactPage") ? true:false;
Do you expect it to test the value of an input field?

var contact = document.getElementById("contactPage").value==='true' ? true:false;

jmrker
03-09-2012, 04:19 PM
It appears that you are checking for an element to exist, not the value of the element.

It is not clear what the element is (checkbox, radio button, textbox, select, etc)
so this is just a sample guess:


var contact = (document.getElementById("contactPage").selectedIndex == '1') ? true:false;
var projects = (document.getElementById("productPage").value != '') ? true:false;
var about = (document.getElementById("aboutPage").checked) ? true:false;
var home = (document.getElementById("homePage").checked == false) ? true:false;


Dang, that blaze4218 types fast!!!

themindco
03-09-2012, 04:38 PM
Apologize. Should have been more precise to what I am doing. It is for nav of the 'section' element.

example at: http://themindspot.com


Trying suggestions now.

DaveyErwin
03-09-2012, 04:45 PM
<style type="text/javascript"><style type="text/javascript">
maybe
<script type="text/javascript">
you need an onload handler to
run the getElementsById code in
maybe
<body onload="init()">

then put your code in
the function

function init(){
var contact = (document.getElementById("contactPage")) ? true:false;
var projects = (document.getElementById("productPage")) ? true:false;
var about = (document.getElementById("aboutPage")) ? true:false;
var home = (document.getElementById("homePage")) ? true:false;

}

themindco
03-09-2012, 04:58 PM
After attempting a few things I would have thought this wold worked but no go.


<script>
var contact = (document.getElementById("contactPage").style.display='block') ? true:false;
var projects = (document.getElementById("productPage").style.display='block') ? true:false;
var about = (document.getElementById("aboutPage").style.display='block') ? true:false;
var homeP = (document.getElementById("homePage").style.display='block') ? true:false;

var checkPage = function (contact, projects, about, homeP)
{
if (contact === true){}
else if(projects === true){}
else if(about === true){}
else if(homeP === false){
document.getElementById("homePage").style.display='block';}
};
</script>

and


<body onLoad="checkPage(contact, projects, about, homeP);">

blaze4218
03-09-2012, 05:09 PM
You are setting the value. Look at my example again for a conditional test

var contact = (document.getElementById("contactPage").style.display='block') ? true:false;
should be

var contact = (document.getElementById("contactPage").style.display=='block') ? true:false;

blaze4218
03-09-2012, 05:12 PM
Again, I still don't understand what you expect to accomplish...

You have left the code brackets for 3 of your conditional tests empty... why?
Should that be
<script>
var contact = (document.getElementById("contactPage").style.display=='block') ? true:false;
var projects = (document.getElementById("productPage").style.display=='block') ? true:false;
var about = (document.getElementById("aboutPage").style.display=='block') ? true:false;
var homeP = (document.getElementById("homePage").style.display=='block') ? true:false;

var checkPage = function (contact, projects, about, homeP)
{
if (contact === true){
document.getElementById("contactPage").style.display='block';}
else if(projects === true){
document.getElementById("productPage").style.display='block';}
else if(about === true){
document.getElementById(aboutPage").style.display='block';}
else if(homeP === false){
document.getElementById("homePage").style.display='block';}
};
</script>?

themindco
03-09-2012, 05:16 PM
I had noticed I did that between post and thought it wold have been the problem but nope again. :)

Made the following modifications:


<script>
var contact = (document.getElementById("contactPage").style.display == 'block') ? true:false;
var projects = (document.getElementById("productPage").style.display == 'block') ? true:false;
var about = (document.getElementById("aboutPage").style.display == 'block') ? true:false;

var checkPage = function (contact, projects, about)
{
if (contact === true){}
else if(projects === true){}
else if(about === true){}
else {
document.getElementById("homePage").style.display='block';}
};
</script>

and


<body onLoad="checkPage(contact, projects, about);">

Figured no need to check for 'homePage'.

blaze4218
03-09-2012, 05:21 PM
When I ask "what do you expect to accomplish?" a link to what you have doesn't answer that. It shows me what you have, and since you claim that the code is *not* working for you, I must assume that what is at the link is not what you expect to accomplish... When I view the page it appears to me that it is working correctly, so what would you like addressed?

(I suppose, if all you wanted was a correction of your code as posted- you have gotten that.)

Also, I don't see that you have made the corrections to your site that you claim to have made...

blaze4218
03-09-2012, 05:28 PM
I can't believe I missed this...

Did you make DaveyErwin's suggested change from post #7? You are running javascript that operates against DOM elements before they are loaded! You must do that after the body loads.

themindco
03-09-2012, 05:32 PM
I apologize if I am frustrating you. What I am trying to accomplish is when the page loads to check if any 'section id's' are currently being set to block. If they are then they do nothing. That's why the if statement is like so. If any statement is true then it does nothing.

And yes you have helped very much. Thank you! I do apologize for not saying so earlier.

blaze4218
03-09-2012, 05:45 PM
It is not necessary to write javascript to execute nothing. Nothing will occur all by itself.

If you would like do something if the value is *not* currently set to block, then you are doing something. I believe this would be of greater value to you.

themindco
03-09-2012, 05:53 PM
Ok. It works for the most part but now the homePage wont go away. :)

blaze4218
03-09-2012, 05:57 PM
I think I get it now...

How about this?

var checkPage = function ()
{
var contact = (document.getElementById("contactPage").style.display == 'block') ? true:false;
var projects = (document.getElementById("productPage").style.display == 'block') ? true:false;
var about = (document.getElementById("aboutPage").style.display == 'block') ? true:false;

if (contact||projects||about){
document.getElementById("homePage").style.display='none';}
else {
document.getElementById("homePage").style.display='block';}
};

themindco
03-09-2012, 06:32 PM
Not sure If I follow you blaze on that last comment. But I posted in the html and css section to see if I could get some more advice. HTML & CSS (http://www.codingforums.com/showthread.php?p=1202517)


<html>
<head></head>
<style>
#homePage {
display:none;}

#contactPage {
display:none;}

#homePage:target {
display:block;}

#contactPage:target {
display:block;}
</style>
<script>
var checkPage = function ()
{
var contact = (document.getElementById("contactPage").style.display === 'block') ? true:false;
var homeP = (document.getElementById("homePage").style.display === 'block') ? true:false;

if (contact === true){}
else if(homeP === true){}
else{
document.getElementById("homePage").style.display='block';}
};
</script>

<body onLoad="checkPage();">

<nav><a href="#homePage">Home</a> | <a href="#contactPage">Contact</a></nave>
<section id="homePage">
I am Home!
</section>
<section id="contactPage">
I want to talk too!
</section>
</body>
</html>

blaze4218
03-09-2012, 06:38 PM
Just try the code, I think it is what you are looking for...



var checkPage = function ()
{
var contact = (document.getElementById("contactPage").style.display == 'block') ? true:false;
var projects = (document.getElementById("productPage").style.display == 'block') ? true:false;
var about = (document.getElementById("aboutPage").style.display == 'block') ? true:false;

if (contact||projects||about){ // this line will test if any of these 3 variable equal true
document.getElementById("homePage").style.display='none';} // if any one of those elements are visible, hide the homepage element
else {
document.getElementById("homePage").style.display='block';} // if nothing else is visible, show the homepage element
};

themindco
03-09-2012, 06:50 PM
Sorry didn't mean I didn't get that last post. That is what I want, shorter code. Thank you but I still have the problem of the homepage section staying in display. I tried doing display:none before with the else if statements and that didnt work either. Any idea why? Seems you had the same idea but I barely ever mess with the web anymore because its so damn quirky and I feel there is something I am missing in the rule sets.

blaze4218
03-09-2012, 07:35 PM
I don't see that you have in fact made the changes, but I went ahead and tested it anyway...

You are using the section tag instead of a div tag. Whatever the problem, this is somehow the cause. I don't know why, maybe it has something to do with the section tag only existing in HTML5...

When I swapped the section tags for div tags your homePage was able to hide.

themindco
03-09-2012, 08:20 PM
Strange. I tried what you said with the div tag but didn't seem to change anything. I hadn't tested outside of chrome, this could be why. Current code is uploaded if your curious: The Mind Co. (http://www.themindspot.com)

blaze4218
03-09-2012, 08:34 PM
sorry, I had also changed the script, and I forgot to write about that :o

What I meant to say was
You decided to test for a value of "block", but the browser indicates that the value is not block... dunno why... maybe you should toLowercase() or something. (originally I thought that you failed to ensure the value in your CSS, but I checked, and that was right... so maybe the lowercase thing...).
I found that checking for "not equal to none" worked.

var checkPage = function ()
{
var contact = (document.getElementById("contactPage").style.display != 'none') ? true:false;
var projects = (document.getElementById("productPage").style.display != 'none') ? true:false;
var about = (document.getElementById("aboutPage").style.display != 'none') ? true:false;

if (contact||projects||about){
document.getElementById("homePage").style.display='none';}
else {
document.getElementById("homePage").style.display='block';}
};

and then after that didn't work, i did the section to div change, and it all came together

themindco
03-10-2012, 01:23 AM
I get what you mean. I tried your code and with the div tag changes and it didnt work. It wouldn't show the homePage class.

Taking your suggestion though I came up with this, and it allowed the homePage to show onLoad. The issue though of the homePage not going away still exist.



<!DOCTYPE html>
<head>
<style>
#homePage {
display:none;}

#aboutPage {
display:none;}

#productPage {
display:none;}

#contactPage {
display:none;}

#homePage:target {
display:block;}

#aboutPage:target {
display:block;}

#productPage:target {
display:block;}

#contactPage:target {
display:block;}
</style>
<script>
var checkPage = function ()
{
var contact = (document.getElementById("contactPage").style.display == 'none') ? true:false;
var projects = (document.getElementById("productPage").style.display == 'none') ? true:false;
var about = (document.getElementById("aboutPage").style.display == 'none') ? true:false;
var homeP = (document.getElementById("homePage").style.display == 'none') ? true:false;

if (contact||projects||about||homeP === false){
document.getElementById("homePage").style.display='block';}
else if (contact||projects||about){
document.getElementById("homePage").style.display='none';}
};
</script>
</head>

<body onLoad="checkPage();">

<nav>
<a href="#homePage">Home</a> |
<a href="#aboutPage">About</a> |
<a href="#productPage">Projects</a> |
<a href="#contactPage">Contact</a>
</nav>

<br><br>

<section id="homePage">
I am Home!
</section>
<section id="aboutPage">
I am a fish.
</section>
<section id="productPage">
Its in a box.
</section>
<section id="contactPage">
I want to talk too!
</section>

</body>
</html>

jmrker
03-10-2012, 03:55 AM
After trying to decipher your script logic, I'm wondering is this is what you are trying to do?


<!DOCTYPE html>
<head>
<style>
#homePage { display:block;}
#aboutPage { display:none;}
#productPage { display:none;}
#contactPage { display:none;}
</style>

<script type="text/javascript">
// From: http://www.codingforums.com/showthread.php?p=1202671#post1202671

function $_(IDS) { return document.getElementById(IDS); }
function checkPage(info) {
var tmp = '';
var sel = document.getElementsByTagName('section');
for (var i=0; i<sel.length; i++) {
if (sel[i].id == info) { tmp = 'block' } else { tmp = 'none' }
sel[i].style.display = tmp;
}
$_(info).style.display = 'block';
}
</script>
</head>

<body>

<nav>
<a href="#" onclick="checkPage('homePage');return false">Home</a> |
<a href="#" onclick="checkPage('aboutPage');return false">About</a> |
<a href="#" onclick="checkPage('productPage');return false">Projects</a> |
<a href="#" onclick="checkPage('contactPage');return false">Contact</a>
</nav>

<br><br>

<section id="homePage"> I am Home! </section>
<section id="aboutPage"> I am a fish. </section>
<section id="productPage"> Its in a box. </section>
<section id="contactPage"> I want to talk too! </section>

</body>
</html>

themindco
03-12-2012, 03:29 AM
Thanks man that was a clever one. You know why when we were setting it my way, why it cant be changed back. Is it how its called into the stack? I did this which also works but like you method a little better(shorter more efficient cross browser).


<!DOCTYPE html>
<head>
<style>
#homePage {
display:none;}

#aboutPage {
display:none;}

#productPage {
display:none;}

#contactPage {
display:none;}

#homePage:target {
display:block;}

#aboutPage:target {
display:block;}

#productPage:target {
display:block;}

#contactPage:target {
display:block;}
</style>
<script>
// start JSON
var ray = {
autoLoad: function (obj) {
var els = document.getElementsByTagName('*'); // Get all elements in DOM
for (var i = 0 ; i < els.length ; i++) {
if (els[i].id.toLowerCase() == obj.id)
location.href = els[i].href;
}
}
}

window.addEventListener?window.addEventListener('load',function () {
ray.autoLoad({id:'autoid'});
},false):window.attachEvent('onload',function() {
ray.autoLoad({id:'autoid'});
}); // IE : FF
</script>
</head>

<body onLoad="onload();">

<nav>
<a id="autoid" href="#homePage">Home</a> |
<a href="#aboutPage">About</a> |
<a href="#productPage">Projects</a> |
<a href="#contactPage">Contact</a>
</nav>

<br><br>

<section id="homePage">
I am Home!
</section>
<section id="aboutPage">
I am a fish.
</section>
<section id="productPage">
Its in a box.
</section>
<section id="contactPage">
I want to talk too!
</section>

</body>
</html>

jmrker
03-12-2012, 03:52 AM
Thanks man that was a clever one. You know why when we were setting it my way, why it cant be changed back. Is it how its called into the stack? I did this which also works but like you method a little better(shorter more efficient cross browser).

...


Ok. It works for the most part but now the homePage wont go away.

I'm a bit confused at this point and don't know to which post each of you are referring to? :confused:

The code I posted works in FF and Chrome (untested in MSIE).
Is there some problem with the code I posted?
If not using the code I posted, show what the problem is with your code or a live link to the problem.

themindco
03-12-2012, 04:05 AM
I'm a bit confused at this point and don't know to which post each of you are referring to? :confused:

The code I posted works in FF and Chrome (untested in MSIE).
Is there some problem with the code I posted?
If not using the code I posted, show what the problem is with your code or a live link to the problem.

That was some sort of ad bot. It has been banned for advertising some images in the post. It just copied one of my earlier post and posted ads.

You code works great infact just got done switching it over. The Mind Co. (http://www.themindspot.com)

Resolved!!

jmrker
03-12-2012, 04:34 AM
That was some sort of ad bot. It has been banned for advertising some images in the post. It just copied one of my earlier post and posted ads.

You code works great infact just got done switching it over. The Mind Co. (http://www.themindspot.com)

Resolved!!

Thanks for the information about the bot.
I get fooled that way a lot!
Anyway, I'm glad your problem is resolved.
Good Luck!
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum