JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 18th, 2012, 09:07 PM
Nanomech's Avatar
Nanomech Nanomech is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: The Pleiades
Posts: 196 Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 23 h 53 m 4 sec
Reputation Power: 7
Send a message via Skype to Nanomech
Question Alternative for javascript, noscript??

Hi, I have an 'about us' page which uses javascript to switch between 'tabs' to display different content. Trouble is, if the user switches off JS in their browser, my page no longer works. I tried using the <noscript> tag to create an alternative but my original content is still being displayed.

Here is my code, which uses the javascript:
Code:
<div id="about_container">
   <div id="tabs">
   
       <div id="tab1">
	      <h4 id="head1" onclick="change_content(1);">History</h4>
	   </div>
	   
	   <div id="tab2">
	      <h4 id="head2" onclick="change_content(2);">General Information</h4>
	   </div>
   </div>
   
   <div id="wrapper">
       
	   <div id="tab1content" class="tabBody">
	      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard 
		  dummy text ever since the 1500sLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has 
		  been the industry's standard dummy text ever since the 1500sLorem Ipsum is simply dummy text of the printing and typesetting 
		  industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500sLorem Ipsum is simply dummy text of the 
		  printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. Lorem Ipsum is 
		  simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since 
		  the 1500s Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard 
		  dummy text ever since the 1500s</p>
		  <img src="/GardenableImproved/images/conservatory.jpg" alt="Workplace" title="Workplace" id="workplace_img" border="0" />
	   </div>
	   
	   <div id="tab2content" class="tabBody">
	      <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by 
		     injected humour, or randomised words which don't look even slightly believable.There are many variations of passages of Lorem 
			 Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't 
			 look even slightly believable.There are many variations of passages of Lorem Ipsum available, but the majority have suffered 
			 alteration in some form, by injected humour, or randomised words which don't look even slightly believable.There are many variations 
			 of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words
			 which don't look even slightly believable.There are many variations of passages of Lorem Ipsum available, but the majority have 
			 suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. There are many 
			 variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or 
			 randomised words which don't look even slightly believable. or randomised words which don't look even slightly believable. There 
			 are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour,
			 or randomised words which don't look even slightly believable.</p>
	   </div>
	   
   </div>
</div>


As you can see I have 'tab1' and 'tab2' which both have a heading 4 inside them, which uses the function to change the content when they are clicked. If javascript is turned off, I wanted to create 2 separate div's containing both sets of information separately.

Hope I explained well enough.

Look forward to your replies.

Regards,

NM.

Reply With Quote
  #2  
Old November 18th, 2012, 11:13 PM
Kravvitz's Avatar
Kravvitz Kravvitz is online now
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
It's not clear to me what you mean by this:
Quote:
If javascript is turned off, I wanted to create 2 separate div's containing both sets of information separately.

Anyway, I would not use noscript elements. The best approach is to use progressive enhancement techniques for writing the JavaScript. I suggest you read these:
You cannot rely on JavaScript being available. Period.
Progressive Enhancement and the Future of Web Design
Accessibility is seldom just up to the interface developer
"This is not another XMLHttpRequest article"

Here's an old demo which does something similar to what you're doing. It's a basic example of progressive enhancement.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
<script type="text/javascript"><!--
function toggleDisplay(elId) {
  if(!document.getElementById) return;
  var el = document.getElementById(elId);
  if(!el || !el.style) return;
  el.style.display = el.style.display=='none'?'':'none';
}

window.onload = function(){
  if(!document.getElementById) return;
  document.getElementById('demoBtn').onclick=function(){
    toggleDisplay('one');
    toggleDisplay('two');
  }
  toggleDisplay('two');
}
// -->
</script>
</head>
<body>

<div><input id="demoBtn" type="button" value="swap elements"></div>
<div id="one">element one</div>
<div id="two">element two</div>

</body>
</html>
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #3  
Old November 19th, 2012, 02:35 AM
Nanomech's Avatar
Nanomech Nanomech is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: The Pleiades
Posts: 196 Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 23 h 53 m 4 sec
Reputation Power: 7
Send a message via Skype to Nanomech
What I mean is that if javascript is turned off, rather than not being able to switch between the content, I wanted to display 2 separate div's containing the relevant information.

Kind regards,

NM.

Reply With Quote
  #4  
Old November 19th, 2012, 03:29 PM
Kravvitz's Avatar
Kravvitz Kravvitz is online now
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Yeah, that's what I thought you meant but I wanted to be sure.

Do you have any questions about my previous reply? One thing I should note (which is not done correctly in the demo I posted), is that if you have a link or button that only works when JavaScript is enabled, you should use JavaScript to add it to the page in the first place.

Reply With Quote
  #5  
Old November 19th, 2012, 04:42 PM
Nanomech's Avatar
Nanomech Nanomech is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: The Pleiades
Posts: 196 Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 23 h 53 m 4 sec
Reputation Power: 7
Send a message via Skype to Nanomech
Yea i like that example you posted. Works exactly how I want, trouble is I can't understand that code which is annoying me right now so I'm gonna scrap JS for that page and just display it as 2 separate div's and 2 separate headings.

Thanks anyway.

Regards,

NM.

Reply With Quote
  #6  
Old November 19th, 2012, 06:54 PM
Kravvitz's Avatar
Kravvitz Kravvitz is online now
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Some intermediate techniques were used in the demo script. I would be happy to try to explain them if you'd like. Is it just the things in the "toggleDisplay" function that are mysteries to you or do you not fully understand the onload event handler either?

Reply With Quote
  #7  
Old November 20th, 2012, 05:58 AM
Nanomech's Avatar
Nanomech Nanomech is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: The Pleiades
Posts: 196 Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 23 h 53 m 4 sec
Reputation Power: 7
Send a message via Skype to Nanomech
I understand the window.onload event handler as I have used it in conjunction with a setInterval() to display/update time every second (on a different site).

What I'll do is, explain what I think is happening, then if you would be so kind to tell me my errors, I would really appreciate it.

Ok so, when the document loads you run an anonymous function. You check to see if document.getElementById() does not return TRUE, if so return.

Just to note, I'm not sure why you haven't passed an ID to the getElementById function....

If we get past this, when the button is clicked we run a function, this function will take in the id of the div elements and swap them accordingly. To finish the onload, if nothing was clicked, we display <div id="two"> by default.

When the button is clicked, we go into another function called toggleDisplay(). This function accepts 1 parameter, the id of the div element. Again we check to see if getElementById doesn't return true and if it doesn't, we return from the function. If we do have the element we carry on.

This is pretty much where I get completely lost and also I don't understand how this works without JS. Like I said though it is exactly the effect I wanted I just hate to copy and paste code which I don't understand how it works because I'll never learn anything!

Kind regards,

NM.

Reply With Quote
  #8  
Old November 20th, 2012, 03:11 PM
Kravvitz's Avatar
Kravvitz Kravvitz is online now
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Quote:
Just to note, I'm not sure why you haven't passed an ID to the getElementById function....

It's a best practice to check for support for a feature before you try to use it (it's often referred to as "object detection"). However, in this case, since it's a DOM Level 1 (DOM1) feature, you have to go back to ancient browsers, like IE4 and Netscape 4, to find a major browser that doesn't support it.

Quote:
To finish the onload, if nothing was clicked, we display <div id="two"> by default.

Not quite. The fact that the onclick handler was defined doesn't matter. Also the element with the ID "two" is hidden by default.

Quote:
This is pretty much where I get completely lost and also I don't understand how this works without JS.

Without JS support enabled, nothing happens. The elements are all visible, because the JS script didn't hide any when the page loaded.

The following line gets (or attempts to anyway) a reference to the element with a given ID and stores it in a the "el" variable. If it fails, the "null" value will be returned.
Code:
var el = document.getElementById(elId);

This line makes sure that such an element exists and that it has the style property/object we need to use to show or hide it:
Code:
if(!el || !el.style) return;

This line uses the "ternary operator" (follow the link for more information), which is just a basic if/else conditional that can be used within a statement, allowing for shorter code (which, in this case, is about half):
Code:
el.style.display = el.style.display=='none'?'':'none';

it's the same as this, which checks if the element is hidden (via a certain method) and if so shows it, otherwise it hides it:
Code:
if(el.style.display=='none'){
  el.style.display = '';
} else {
  el.style.display = 'none';
}


Does that more sense to you now?

Last edited by Kravvitz : November 20th, 2012 at 03:13 PM.

Reply With Quote
  #9  
Old November 20th, 2012, 05:29 PM
Nanomech's Avatar
Nanomech Nanomech is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: The Pleiades
Posts: 196 Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 23 h 53 m 4 sec
Reputation Power: 7
Send a message via Skype to Nanomech
Ah ok I never knew you could check for support using that. I'll remember that one!

Riiight so because the JavaScript is what set's the display to none (depending on certain things), is it's turned off, both are displayed because the 'display' code never executes.

I am aware of ternary operators and used them before in PHP.

so it's basically saying, if this element's display attribute is equal to 'none' already, do nothing, else, set it to none...

Thanks for your help to understand the code. I have a better understanding now.

Kind regards,

Nm.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Alternative for javascript, noscript??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap