Click to See Complete Forum and Search --> : If/Else Statement
i'm just about ready to start my first javascript project, but there's one thing i need to know before i do it.
is it possible to make and if/else statement where if the else part is the active part that the javascript will exit and go into regular html mode so that in the 20% or so chance cae that the user has js disabled, they'll still be able to see the site? and if so, if the if part is the active part, will it do that and then the regular html part or just the if part?
bradgrafelman
03-12-2006, 12:07 AM
I'm really trying to understand what you're saying... honestly... but I can't.
You mentioned "if the user has JS disabled". If they have it disabled, the entire script won't do a single thing. Instead, you might look at using NOSCRIPT tags.
read post #3 for explaination (http://www.webdeveloper.com/forum/showthread.php?t=98673)
and then post #7 if u understand
Weedpacket
03-12-2006, 02:20 AM
bradgrafelman has already given you your answer.
madwormer2
03-12-2006, 08:41 AM
Yeh, you seem to believe that if javascript is disabled... it'll run the javascript else statement.
But it wont... because if javascript is disabled, it'll just skip over it completely.
JPnyc
03-12-2006, 09:58 AM
The way one deals with this is, you create a regular HTML version of the site that includes a javascript redirect to the javascript version. If they have it disabled, they see the HTML version since the redirect will do nothing. And obviously if they have it enabled they'll be redirected to the JS version
dream.scape
03-12-2006, 12:32 PM
use the noscript tag to catch people with javascript disabled if you want to display something different to them.
<script type="text/javascript">
//<![CDATA[
document.write('<p>You have javascript enabled<\/p>');
//]]>
</script>
<noscript>
<p>You have havascript disabled</p>
</noscript>
JPnyc
03-12-2006, 02:35 PM
Bit cumbersome if he wants to code an entire JS free version of the site though. Means longer load time for everyone, if the page is complex and has a fair amount of code.
dream.scape
03-12-2006, 04:00 PM
Bit cumbersome if he wants to code an entire JS free version of the site though. Means longer load time for everyone, if the page is complex and has a fair amount of code.
Seeing as though he's been extremely vague on what he wants to do, I thought I'd put it out there.
JPnyc
03-12-2006, 04:47 PM
Agreed.
ok, here's is what i'm trying to do, i'm trying to make it so that if your screen res is smaller than 1024x768, a lower res version will be displyed AUTOMATICALLY
no here's the thing, i can make it so that if the user has javascript disabled, they will just get the regular, pure html 1024x768 version. that's the easy part. the problem is, i don't know if it's possible so that if js is enabled, they will only get the version that corresponds with their screen res instead of both tables
JPnyc
03-12-2006, 07:03 PM
Using the method I mentioned, you can. Put the screen width detect script with a redirect into the 1024 version. If they dont have JS enabled, it will do nothing and they'll just get the 1024 version.
JPnyc
03-12-2006, 07:03 PM
But why not just make the table width a % value? Doesn't that take care of it with no procedural coding?
yes, but then for any res larger than 1024, the images will be distorted. the page is graphical, not tables and colors
i would do ur idea, but i really want this to be 100% automatic. or, wait, r u saying that i make 2 versions of each page, and the js will automatically redirect to the % page?
JPnyc
03-12-2006, 10:22 PM
Could do that, or you could use a JS function to change the images and table size if they have JS enabled and the res is a certain size. Whichever way you think is easier
can you walk me through that second thing u said?
JPnyc
03-13-2006, 11:48 AM
Why don't you give me an url to the page so I can see exactly which approach is best.
it's not up yet and i don't have the ftp account
the best i'd be able to show you is a gif of the whole thing
JPnyc
03-13-2006, 12:40 PM
Well that won't help. Let's try the reverse approach. Which of the methods mentioned do you feel you have the best shot at doing? There's not a whole lot of difference between them so whichever you feel you have the skills to do is the one you should try.
i'd be able to do them both just as esily, but i'd prefer to make it all happen on a single page so can you tell me how to do the second thing you said?
or if you want could upload temporarily into my personal ftp account so you could see
JPnyc
03-13-2006, 01:49 PM
Well the other is actually easier. How many images are we talking about here?
JPnyc
03-13-2006, 02:30 PM
Ok since you're not around to answer that question, I'll give you the 2 different scenarios.
1) if you have just a few pics to change the working statement will be something like;
document.images[0].src="bigPic.jpg"; // this is for the 1st image on the page
document.images[1].src="bigPic2.jpg"; // this is for the 2nd img on the page
That's if you have just a few. Now if you have LOTS, then you create 2 arrays, 1 populated with the smaller pics, another with the larger ones, and you use a loop to iterate through both the images object and the array holding the pics.
explain the one that i do if i have alot
JPnyc
03-13-2006, 03:49 PM
Well you make 2 arrays like I said, and when you do your screensize test, you set one or the other to the src of the image, using a for-loop.
for( var i = 0; i<bigPic.length; i++) {
document.images[i].src = bigPic[i];
}
um, did i mention that i only started learning js on friday?
JPnyc
03-14-2006, 05:48 PM
No, but that would make this approach too complex. So go with the 1st idea, use something like
if(screen.availWidth>1024){
window.location.href="bigVersion.htm";
}
well, i guess i'll ask the person i'm making the site for if he'd mind if it were a redirect
JPnyc
03-15-2006, 11:07 AM
It shouldn't matter that much since there's no performance difference between the 2 approaches.
i know, but it's his site after all so i'll just clear it up
but i would like to use this script for my site so can explain the other option?
JPnyc
03-15-2006, 05:35 PM
I'm afraid I can't explain it more than I already have without writing it for you, and work is just too busy to allow that much time. Sorry.
can you link me to a tut on it or something like that?
JPnyc
03-18-2006, 07:19 PM
Wish I could, but I don't know of one. If you're learning JS, start smaller. You need to know the language to write something like this.
i know the basics, and i'm pretty good at java, even though they're not the same, they're not that far off
actually, now that i think about it, is there a php script to detect if the user has js disabled, or would that be a client-side thing?
JPnyc
03-19-2006, 04:52 PM
The basic syntax is the same but that's pretty much it. Java is a full-blown OOL. Javascript is among the simplest scripting languages around. Very forgiving.
I found this (http://techpatterns.com/downloads/browser_detection.php) on a search. I have no idea about it other than what it says on the page.
nice find, i'll check it out
thx for ur help
oh, and um, wut does OOL stand for?
JPnyc
03-19-2006, 07:26 PM
Object Oriented Language, and you're very welcome.
PHP Builder
Copyright WebMediaBrands Inc. All Rights Reserved.