/    Sign up×
Community /Pin to ProfileBookmark

Changing to a ‘prototype’ syntax?

I have a simple “Tabulature” type display that works great
with help from some forum members in a different thread.
(If interested see: [url]http://www.webdeveloper.com/forum/showthread.php?t=182815[/url])

I thought I should be able to use it in different places on the same page,
and created a dual display with different information.

That too was not too problematic but I can forsee difficulties if I use it too many more times.

Could someone point me in the right direction to change the following example to a prototype syntax?

I’m just not clear on what should be methods and which should be functions.
I’m not too clear where to start in trying to prototype the functions and arrays.

The main HTML script looks like:

[code=php]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html lang=”en”>
<head>
<title>Basic TAB display</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>

<script type=”text/javascript” src=”Tabs.js”></script>
<script type=”text/javascript” src=”Tabs1.js”></script>

<script type=”text/javascript”>
function ClrTabs(ArrayName,BtnName) {
var ids;
for (i=0; i<ArrayName.length; i++) {
ids = BtnName+i;
document.getElementById(ids).style.backgroundColor = “#FFFFFF”;
}
}
function Show(ArrayName,BtnName,N,Display) {
ClrTabs(ArrayName,BtnName);
document.getElementById(BtnName+N).style.backgroundColor = “#FFFF00″;
document.getElementById(Display).innerHTML = ArrayName[N];
}
</script>

<style type=”text/css”>
#TabInfo, #TabInfo1 {
border: 1px solid blue;
height:200px;
overflow:auto;
margins: 0px;
padding: 0px;
}
</style>

</head>
<body onload=”Show(Info,’btnTab’,0,’TabInfo’);Show(Info1,’btn1Tab’,0,’TabInfo1′)”>

<div style=”width:300px;float:left;margin:10px;”>
<div>
<button id=”btnTab0″ onclick=”Show(Info,’btnTab’,0,’TabInfo’)”>&nbsp</button>
<button id=”btnTab1″ onclick=”Show(Info,’btnTab’,1,’TabInfo’)”>Tab 1</button>
<button id=”btnTab2″ onclick=”Show(Info,’btnTab’,2,’TabInfo’)”>Tab 2</button>
<button id=”btnTab3″ onclick=”Show(Info,’btnTab’,3,’TabInfo’)”>Tab 3</button>
<button id=”btnTab4″ onclick=”Show(Info,’btnTab’,4,’TabInfo’)”>Tab 4</button>
</div>
<div id=”TabInfo”></div>
</div>

<div style=”width:350px;float:left;margin:10px”>
<div>
<button id=”btn1Tab0″ onclick=”Show(Info1,’btn1Tab’,0,’TabInfo1′)”>&nbsp</button>
<button id=”btn1Tab1″ onclick=”Show(Info1,’btn1Tab’,1,’TabInfo1′)”>Introduction</button>
<button id=”btn1Tab2″ onclick=”Show(Info1,’btn1Tab’,2,’TabInfo1′)”>Background</button>
<button id=”btn1Tab3″ onclick=”Show(Info1,’btn1Tab’,3,’TabInfo1′)”>Design</button>
<button id=”btn1Tab4″ onclick=”Show(Info1,’btn1Tab’,4,’TabInfo1′)”>Data collection</button>
<button id=”btn1Tab5″ onclick=”Show(Info1,’btn1Tab’,5,’TabInfo1′)”>Analysis</button>
<button id=”btn1Tab6″ onclick=”Show(Info1,’btn1Tab’,6,’TabInfo1′)”>Conclusions</button>
</div>
<div id=”TabInfo1″></div>
</div>

<div style=”clear:both”></div>

</body>
</html>
[/code]

And the two JAVASCRIPT example extension files called look like this:

[code=php]
// Tabs.js

var Info = new Array();
var str = ”;
str = “Tab Info #0”; Info[0] = str;
str = “Tab Info #1”; Info[1] = str;
str = “Tab Info #2”; Info[2] = str;
str = “Tab Info #3”; Info[3] = str;
str = “Tab Info #4”; Info[4] = str;
[/code]

[code=php]
// Tabs1.js

var Info1 = new Array();
var str = ”;
// for testing purposes
str = ‘<h1 align=”center”>Abstract <br>Information</h1>’; Info1[0] = str;
str = ‘<h2 align=”center”>Introduction <br>Information</h2>’;
for (i=0; i<20; i++) { str += ‘Info #’+i+'<br>’; } Info1[1] = str;
str = ‘<h3 align=”center”>Background <br>Information</h3>’;
for (i=0; i<10; i++) {
str += ‘<em>Now is the time for all good men to come to the aid of their country.</em> ‘;
} Info1[2] = str;
//
str = “<h3 align=’center’>Design</h3>”; Info1[3] = str;
str = “<h3 align=’center’>Data collection</h3>”; Info1[4] = str;
str = “<h3 align=’center’>Analysis</h3>”; Info1[5] = str;
str = “<h3 align=’center’>Conclusions</h3>”; Info1[6] = str;
[/code]

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991May 31.2008 — This isn't exactly what you are asking, but here is a sample I had from before that I've slightly altered.[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Basic TAB display</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function tabs(id,content){
var con = document.getElementById(id);
this.div = con.getElementsByTagName("div")[0];
this.buttons = con.getElementsByTagName("button");
this.info = content;
}
tabs.prototype = {
prepare:function(){
for (var i = this.buttons.length-1; i >= 0; i--) {
this.buttons[i].onclick = function(e){
var t = e?e.target:window.event.srcElement;
//window[t.parentNode.parentNode.parentNode.id].show(t.innerHTML);
window[t.parentNode.parentNode.parentNode.id].show(t.innerHTML);
};
}
this.show(this.buttons[0].innerHTML);
return this;
},
undo:function(a){
var r;
for (var i = this.buttons.length-1; i >= 0; i--) {
if (this.buttons[i].innerHTML === a) {
this.buttons[i].style.background = "#ff0";
r = i;
}
else {
this.buttons[i].style.background = "#fff";
}
}
return r;
},
show:function(i){
this.div.innerHTML = this.info[this.undo(i)];
}
};

window.onload = function(){
window.tabs1 = new tabs("tabs1",["1","2","3","4","5"]);
tabs1.prepare();
}

</script>
</head>
<body><div id="tabs1">
<ul>
<li><button>&nbsp;</button></li>
<li><button>Tab 1</button></li>
<li><button>Tab 2</button></li>
<li><button>Tab 3</button></li>
<li><button>Tab 4</button></li>
</ul>
<div></div>
</div>

</body>
</html> [/code]

If there is anything you don't understand in the above code, ask. It's not that elegant, but it's functional. The only drawback is that the id on the div must be the same as the variable that holds the new tabs() object. That is generally the way you would do it, and as you can see, the prototypes are the prepare, undo, and show functions, which don't need to be different, and the other things (like the content and stuff), that does change, is stored as instance variables.
Copy linkTweet thisAlerts:
@JMRKERauthorJun 01.2008 — @Declan1991, ?

Thanks for the example. I was able to (I think) figure out how the prototype worked and applied it to the code of post #1.


It uses the previous JS files for the 'tab contents' with this new version of the calling HTML.


I liked this way as I can change the contents of the display without changing the operating script and should be able to apply the concept to additional tab display boxes.

I welcome any comments on how to make it better now that I have a version that I can understand and expand upon. Thanks again. ?

[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Prototype TAB Display</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style type="text/css">
.TabContent {
border: 1px solid blue;
height:200px;
overflow:auto;
margins: 0px;
padding: 5px;
}
</style>

<script type="text/javascript" src="Tabs.js"></script>
<script type="text/javascript" src="Tabs1.js"></script>

<script type="text/javascript">
function TabBox() {}

TabBox.prototype = {
ClrTabs : function (ArrayName,BtnName) {

var ids;
for (i=0; i<ArrayName.length; i++) {
ids = (BtnName+i);
document.getElementById(ids).style.backgroundColor = "#FFFFFF";
}
},
Show : function (ArrayName,IDS,Display) {
var BtnName = IDS.substring(0,IDS.length-1);
var N = IDS.charAt(IDS.length-1);
this.ClrTabs(ArrayName,BtnName);
document.getElementById(IDS).style.backgroundColor = "#FFFF00";
document.getElementById(Display).innerHTML = ArrayName[N];
}
}

TBox1 = new TabBox;
TBox2 = new TabBox;
function InitTabs() {
TBox1.Show(Info,'btn1Tab0','TabInfo1');
TBox2.Show(Info1,'btn2Tab0','TabInfo2');
}

</script>


</head>
<body onload="InitTabs()">

<div style="width:300px;float:left;margin:10px;">
<div>
<button id="btn1Tab0" onclick="TBox1.Show(Info,this.id,'TabInfo1')">&nbsp</button>
<button id="btn1Tab1" onclick="TBox1.Show(Info,this.id,'TabInfo1')">Tab 1</button>
<button id="btn1Tab2" onclick="TBox1.Show(Info,this.id,'TabInfo1')">Tab 2</button>
<button id="btn1Tab3" onclick="TBox1.Show(Info,this.id,'TabInfo1')">Tab 3</button>
<button id="btn1Tab4" onclick="TBox1.Show(Info,this.id,'TabInfo1')">Tab 4</button>
</div>
<div class="TabContent" id="TabInfo1"></div>
</div>

<div style="width:315px;float:left;margin:10px;">
<div>
<button id="btn2Tab0" onclick="TBox2.Show(Info1,this.id,'TabInfo2')">&nbsp</button>
<button id="btn2Tab1" onclick="TBox2.Show(Info1,this.id,'TabInfo2')">Introduction</button>
<button id="btn2Tab2" onclick="TBox2.Show(Info1,this.id,'TabInfo2')">Background</button>
<button id="btn2Tab3" onclick="TBox2.Show(Info1,this.id,'TabInfo2')">Design</button>
<button id="btn2Tab4" onclick="TBox2.Show(Info1,this.id,'TabInfo2')">Data collection</button>
<button id="btn2Tab5" onclick="TBox2.Show(Info1,this.id,'TabInfo2')">Analysis</button>
<button id="btn2Tab6" onclick="TBox2.Show(Info1,this.id,'TabInfo2')">Conclusions</button>
</div>
<div class="TabContent" style="height:178px" id="TabInfo2"></div>
</div>

<div style="clear:both"></div>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@vwphillipsJun 01.2008 — difficult to see what advatage prototyping has for such a simple requirement

[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Basic TAB display</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript" src="Tabs.js"></script>
<script type="text/javascript" src="Tabs1.js"></script>
<script language="JavaScript" type="text/javascript">
<!--
// Tabs.js

var Info = new Array();
var str = '';
str = "Tab Info #0"; Info[0] = str;
str = "Tab Info #1"; Info[1] = str;
str = "Tab Info #2"; Info[2] = str;
str = "Tab Info #3"; Info[3] = str;
str = "Tab Info #4"; Info[4] = str;
// Tabs1.js

var Info1 = new Array();
var str = '';
// for testing purposes
str = '<h1 align="center">Abstract <br>Information</h1>'; Info1[0] = str;
str = '<h2 align="center">Introduction <br>Information</h2>';
for (i=0; i<20; i++) { str += 'Info #'+i+'<br>'; } Info1[1] = str;
str = '<h3 align="center">Background <br>Information</h3>';
for (i=0; i<10; i++) {
str += '<em>Now is the time for all good men to come to the aid of their country.</em> ';
} Info1[2] = str;
//
str = "<h3 align='center'>Design</h3>"; Info1[3] = str;
str = "<h3 align='center'>Data collection</h3>"; Info1[4] = str;
str = "<h3 align='center'>Analysis</h3>"; Info1[5] = str;
str = "<h3 align='center'>Conclusions</h3>"; Info1[6] = str; //-->
</script>
<script type="text/javascript">


function Show(ArrayName,BtnName,N,Display) {
var zxcas=document.getElementById(BtnName).getElementsByTagName('INPUT');
for (var zxc0=0;zxc0<zxcas.length;zxc0++){
zxcas[zxc0].style.backgroundColor =zxc0==N?"#FFFF00":"#FFFFFF";
}
document.getElementById(Display).innerHTML = ArrayName[N];
}
</script>

<style type="text/css">
#TabInfo, #TabInfo1 {
border: 1px solid blue;
height:200px;
overflow:auto;
margins: 0px;
padding: 0px;
}
</style>

</head>
<body onload="Show(Info,'btnTab',0,'TabInfo');Show(Info1,'btn1Tab',0,'TabInfo1')">

<div style="width:300px;float:left;margin:10px;">
<div id="btnTab">
<input type="button" onclick="Show(Info,'btnTab',0,'TabInfo')" value="&nbsp" >
<input type="button" onclick="Show(Info,'btnTab',1,'TabInfo')" value="Tab 1" >
<input type="button" onclick="Show(Info,'btnTab',2,'TabInfo')" value="Tab 2" >
<input type="button" onclick="Show(Info,'btnTab',3,'TabInfo')" value="Tab 3" >
<input type="button" onclick="Show(Info,'btnTab',4,'TabInfo')" value="Tab 4" >
</div>
<div id="TabInfo"></div>
</div>

<div style="width:350px;float:left;margin:10px">
<div id="btn1Tab">
<input type="button" onclick="Show(Info1,'btn1Tab',0,'TabInfo1')" value="&nbsp" >
<input type="button" onclick="Show(Info1,'btn1Tab',1,'TabInfo1')" value="Introduction" >
<input type="button" onclick="Show(Info1,'btn1Tab',2,'TabInfo1')" value="Background" >
<input type="button" onclick="Show(Info1,'btn1Tab',3,'TabInfo1')" value="Design" >
<input type="button" onclick="Show(Info1,'btn1Tab',4,'TabInfo1')" value="Data collection" >
<input type="button" onclick="Show(Info1,'btn1Tab',5,'TabInfo1')" value="Analysis" >
<input type="button" onclick="Show(Info1,'btn1Tab',6,'TabInfo1')" value="Conclusions" >
</div>
<div id="TabInfo1"></div>
</div>

<div style="clear:both"></div>
</form>

</body>
</html>[/CODE]


perhaps it is a learning exercise
Copy linkTweet thisAlerts:
@JMRKERauthorJun 01.2008 — 
perhaps it is a learning exercise
[/QUOTE]

That was the primary goal. ?

Now I think I can incorporate several different version onto a page that

would allow me to have multiple selections within a group of associated topics.

For me it organizes my thoughts a little better and I learned a lot more

about prototype coding than I did before the attempt. Any thoughts about

improvements are welcome, even if you see no immediate benefits at this time.
Copy linkTweet thisAlerts:
@vwphillipsJun 01.2008 — improvements are welcome,[/QUOTE]

note the use of the DOM in my example to simplify the code.

also

[CODE]// Tabs.js

var Info = new Array();
var str = '';
Info[0] = "Tab Info #0";
Info[1] = "Tab Info #1";
Info[2] = "Tab Info #2";
Info[3] = "Tab Info #3";
Info[4] = "Tab Info #4";
// Tabs1.js

var Info1 = new Array();
var str = '';
// for testing purposes
Info1[0] ='<h1 align="center">Abstract <br>Information</h1>';
Info1[1] ='<h2 align="center">Introduction <br>Information</h2>';
for (var i=0; i<20; i++) { Info1[1]+= 'Info #'+i+'<br>'; }
Info1[2] = '<h3 align="center">Background <br>Information</h3>';
for (var j=0; j<10; j++) {
Info1[2]+= '<em>Now is the time for all good men to come to the aid of their country.</em> ';
}
//
Info1[0] ="<h3 align='center'>Design</h3>";
Info1[0] ="<h3 align='center'>Data collection</h3>";
Info1[0] ="<h3 align='center'>Analysis</h3>";
Info1[0] ="<h3 align='center'>Conclusions</h3>";

[/CODE]
Copy linkTweet thisAlerts:
@Declan1991Jun 01.2008 — Bit of [url=http://www.json.org/]JSON[/url] might help reduce file size:var Info = ["Tab Info #0","Tab Info #1","Tab Info #2","Tab Info #3","Tab Info #4"];
Copy linkTweet thisAlerts:
@JMRKERauthorJun 01.2008 — @vwphillips:

I liked the code simplification with the getElementsByTagName()

and the use of the teriary command. I incorporated that into my

next version and removed the superfulous ClrTab() function.

I'm not sure what is different in the [ code ] section in the rest of the post #6

as it looks to contain the same as I have in the external JS files.


Maybe my old eyes just don't see the difference. ?



@Declan1991:

I liked the idea of using JSON, but for this example I not sure I understand the benefits. The array content you provided for the array:
<i>
</i>var Info = ["Tab Info #0","Tab Info #1","Tab Info #2","Tab Info #3","Tab Info #4"];

does not seem to lend itself to larger content amounts. While the 'Tabs1.js' is just a stupid example to fill-up the tab contents using HTML tags, how would the initialization be different or an improvement using JSON as an external file.

?



For purposes of discussion, this is the current working version: ?
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Prototype TAB Display</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style type="text/css">
.TabContent {
border: 1px solid blue;
height:200px;
overflow:auto;
margins: 0px;
padding: 5px;
}
</style>

<script type="text/javascript" src="Tabs0.js"></script>
<script type="text/javascript" src="Tabs1.js"></script>

<script type="text/javascript">
function TabBox() {}

TabBox.prototype = {
Show : function (ArrayName,IDS,Display) {
var BtnName = IDS.substring(0,IDS.length-1);
var N = IDS.charAt(IDS.length-1);
var zxcas=document.getElementById(IDS).getElementsByTagName('INPUT');
for (var zxc0=0;zxc0<zxcas.length;zxc0++){
zxcas[zxc0].style.backgroundColor =zxc0==N?"#FFFF00":"#FFFFFF";
}
document.getElementById(Display).innerHTML = ArrayName[N];
}
}

TBox1 = new TabBox;
TBox2 = new TabBox;
function InitTabs() {
TBox1.Show(Info,'btn1Tab0','TabInfo1');
TBox2.Show(Info1,'btn2Tab0','TabInfo2');
}
</script>

</head>
<body onload="InitTabs()">

<div style="width:300px;float:left;margin:10px;">
<div>
<input type="button" id="btn1Tab0" onclick="TBox1.Show(Info,this.id,'TabInfo1')" value="&nbsp;">
<input type="button" id="btn1Tab1" onclick="TBox1.Show(Info,this.id,'TabInfo1')" value="Tab 1">
<input type="button" id="btn1Tab2" onclick="TBox1.Show(Info,this.id,'TabInfo1')" value="Tab 2">
<input type="button" id="btn1Tab3" onclick="TBox1.Show(Info,this.id,'TabInfo1')" value="Tab 3">
<input type="button" id="btn1Tab4" onclick="TBox1.Show(Info,this.id,'TabInfo1')" value="Tab 4">
</div>
<div class="TabContent" id="TabInfo1"></div>
</div>

<div style="width:315px;float:left;margin:10px;">
<div>
<input type="button" id="btn2Tab0" onclick="TBox2.Show(Info1,this.id,'TabInfo2')" value="&nbsp;">
<input type="button" id="btn2Tab1" onclick="TBox2.Show(Info1,this.id,'TabInfo2')" value="Introduction">
<input type="button" id="btn2Tab2" onclick="TBox2.Show(Info1,this.id,'TabInfo2')" value="Background">
<input type="button" id="btn2Tab3" onclick="TBox2.Show(Info1,this.id,'TabInfo2')" value="Design">
<input type="button" id="btn2Tab4" onclick="TBox2.Show(Info1,this.id,'TabInfo2')" value="Data contents">
<input type="button" id="btn2Tab5" onclick="TBox2.Show(Info1,this.id,'TabInfo2')" value="Analysis">
<input type="button" id="btn2Tab6" onclick="TBox2.Show(Info1,this.id,'TabInfo2')" value="Conclusions">
</div>
<div class="TabContent" style="height:178px" id="TabInfo2"></div>
</div>

<div style="clear:both"></div>

</body>
</html>
[/code]

And the external file are:
[code=php]
// Tabs0.js
/*
var Info = new Array();
var str = '';
str = "Tab Info #0"; Info[0] = str;
str = "Tab Info #1"; Info[1] = str;
str = "Tab Info #2"; Info[2] = str;
str = "Tab Info #3"; Info[3] = str;
str = "Tab Info #4"; Info[4] = str;
*/
var Info = ["Tab Info #0","Tab Info #1","Tab Info #2","Tab Info #3","Tab Info #4"];
[/code]


[code=php]
// Tabs1.js
var Info1 = new Array();
var str = '';
// for testing purposes
str = '<h1 align="center">Abstract <br>Information</h1>'; Info1[0] = str;
str = '<h2 align="center">Introduction <br>Information</h2>';

for (i=0; i<20; i++) { str += 'Info #'+i+'<br>'; } Info1[1] = str;
str = '<h3 align="center">Background <br>Information</h3>';

for (i=0; i<10; i++) {
str += '<em>Now is the time for all good men to come to the aid of their country.</em> ';
} Info1[2] = str;
//
str = "<h3 align='center'>Design</h3>"; Info1[3] = str;
str = "<h3 align='center'>Data collection</h3>"; Info1[4] = str;
str = "<h3 align='center'>Analysis</h3>"; Info1[5] = str;
str = "<h3 align='center'>Conclusions</h3>"; Info1[6] = str;
[/code]

Adding additional TAB displays is pretty simple using a new <DIV> section and appending a new JS tab content file.

Thanks again. ?
Copy linkTweet thisAlerts:
@vwphillipsJun 02.2008 — it looks to contain the same as I have in the external JS files. [/QUOTE]

there is no need for the str variable which is messy
Copy linkTweet thisAlerts:
@Declan1991Jun 02.2008 — And you are still defeating the purpose of having prototypes. The whole idea (well in my view) is that you store everything as instance variables, and then common functions and variables as prototypes. All you have really done is move two functions out of global space and into an object.

With objects, you have the opportunity to become very unobtrusive, because you can store a lot of information as instance variables, and the common functions as prototypes. As vwphillips saysdifficult to see what advantage prototyping has for such a simple requirement unless it is a learning experience[/quote] and even so it is a bad learning experience because you are not using objects in a realistic way.
Copy linkTweet thisAlerts:
@JMRKERauthorJun 02.2008 — OK, @vwphillips and @Declan1991, I'll give it some more thought and post what I come up with for more comment. Still learning ...

Thanks for the thoughts!
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...