kamkam
12-27-2006, 11:11 AM
Hello; :p :confused: :p
I got a problem to understand a program which use for users to simply format their post, and they realted with the prototype object.
i got some question from the following code;
1) window.undefined = window.undefined;
this.initDone = false;
return this._target == undefined;
The above all from the follwoing code which i had highlighted.
does the undefined and initDone and _target are prebuild property, if so, but i could not find them http://www.w3schools.com/js/default.asp to see what they do. or they just named by user.
2)what the following function do?
var BBCode = function(){
window.undefined = window.undefined;
this.initDone = false;
}
Does it create a new instance of BBCode object?
what does the window.undefined = window.undefined; and this.initDone = false; do?
3)
BBCode.prototype.init = function(t){
if(this.initDone) // what does it means here?
return false;
if(t == undefined) // what does it means here?
return false;
this._target = t? document.getElementById(t) : t; // what does it means here?
this.initDone = true;
return true;
}
4)
BBCode.prototype.noForm = function(){
return this._target == undefined; // what does it means here?
}
<html>
<head>
<script type="text/javascript">
var BBCode = function(){
window.undefined = window.undefined;
this.initDone = false;
}
BBCode.prototype.init = function(t){
if(this.initDone)
return false;
if(t == undefined)
return false;
this._target = t? document.getElementById(t) : t;
this.initDone = true;
return true;
}
BBCode.prototype.noForm = function(){
return this._target == undefined;
}
</script>
</head>
<body>
<script type="text/javascript">
//////////////////////////////////////////////////
// insertcode is used for bold, italic, underline and quote and just
// wraps the tags around a selection or prompts the user for some
// text to apply the tag to
BBCode.prototype.insertCode = function (tag, desc, endtag) {
if(this.noForm()) return false;
var isDesc = (desc == undefined ¦¦ desc == '')? false : true;
// our textfield
var textarea = this._target;
// our open tag
var open = '['+tag+']';
var close = '[/'+((endtag == undefined)? tag : endtag)+']';
if (!textarea.setSelectionRange) {
var selected = document.selection.createRange().text;
if (selected.length<=0) {
// no text was selected so prompt the user for some text
textarea.value += open+((isDesc)? prompt("Please enter the text you'd like to "+desc, "")
+close : '');
}else{
// put the code around the selected text
document.selection.createRange().text = open+selected+((isDesc)? close : '');
}
} else {
// the text before the selection
var pretext = textarea.value.substring(0, textarea.selectionStart);
// the selected text with tags before and after
var codetext = open+textarea.value.substring(textarea.selectionStart, textarea.selectionEnd)+
((isDesc)? close : '');
// the text after the selection
var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
// check if there was a selection
if (codetext == open+close) {
//prompt the user
codetext = open+((isDesc)? prompt("Please enter the text you'd like to "+desc, "")+close : '');
}
// update the text field
textarea.value = pretext+codetext+posttext;
}
// set the focus on the text field
textarea.focus();
}
//////////////////////////////////////////////////////////
// inserts an image by prompting the user for the url
BBCode.prototype.insertImage = function (html) {
if(this.noForm()) return false;
var src = prompt('Please enter the url', 'http://');
this.insertCode('img='+src);
}
/////////////////////////////////////////////////////////////////
// inserts a link by prompting the user for a url
BBCode.prototype.insertLink = function (html) {
if(this.noForm()) return false;
this.insertCode('url='+prompt("Please enter the url", "http://"), 'as text of the link', 'url')
}
</script>
<script type="text/javascript">
var bb = new BBCode();
</script>
<input type="button" onclick="bb.insertCode('B', 'bold');" value="B" title="Bold text" />
<input type="button" onclick="bb.insertCode('I', 'make italic');" value="I" title="Italic text" />
<input type="button" onclick="bb.insertCode('U', 'underline');" value="U" title="Underlined text" />
<input type="button" onclick="bb.insertImage();" value="Image" title="Inset an image" />
<input type="button" onclick="bb.insertLink();" value="Link" title="Insert a link" /><br />
<textarea id="ttt" name="ttt" rows="20"></textarea>
<script type="text/javascript">
bb.init('ttt');
</script>
</body>
</html>
I got a problem to understand a program which use for users to simply format their post, and they realted with the prototype object.
i got some question from the following code;
1) window.undefined = window.undefined;
this.initDone = false;
return this._target == undefined;
The above all from the follwoing code which i had highlighted.
does the undefined and initDone and _target are prebuild property, if so, but i could not find them http://www.w3schools.com/js/default.asp to see what they do. or they just named by user.
2)what the following function do?
var BBCode = function(){
window.undefined = window.undefined;
this.initDone = false;
}
Does it create a new instance of BBCode object?
what does the window.undefined = window.undefined; and this.initDone = false; do?
3)
BBCode.prototype.init = function(t){
if(this.initDone) // what does it means here?
return false;
if(t == undefined) // what does it means here?
return false;
this._target = t? document.getElementById(t) : t; // what does it means here?
this.initDone = true;
return true;
}
4)
BBCode.prototype.noForm = function(){
return this._target == undefined; // what does it means here?
}
<html>
<head>
<script type="text/javascript">
var BBCode = function(){
window.undefined = window.undefined;
this.initDone = false;
}
BBCode.prototype.init = function(t){
if(this.initDone)
return false;
if(t == undefined)
return false;
this._target = t? document.getElementById(t) : t;
this.initDone = true;
return true;
}
BBCode.prototype.noForm = function(){
return this._target == undefined;
}
</script>
</head>
<body>
<script type="text/javascript">
//////////////////////////////////////////////////
// insertcode is used for bold, italic, underline and quote and just
// wraps the tags around a selection or prompts the user for some
// text to apply the tag to
BBCode.prototype.insertCode = function (tag, desc, endtag) {
if(this.noForm()) return false;
var isDesc = (desc == undefined ¦¦ desc == '')? false : true;
// our textfield
var textarea = this._target;
// our open tag
var open = '['+tag+']';
var close = '[/'+((endtag == undefined)? tag : endtag)+']';
if (!textarea.setSelectionRange) {
var selected = document.selection.createRange().text;
if (selected.length<=0) {
// no text was selected so prompt the user for some text
textarea.value += open+((isDesc)? prompt("Please enter the text you'd like to "+desc, "")
+close : '');
}else{
// put the code around the selected text
document.selection.createRange().text = open+selected+((isDesc)? close : '');
}
} else {
// the text before the selection
var pretext = textarea.value.substring(0, textarea.selectionStart);
// the selected text with tags before and after
var codetext = open+textarea.value.substring(textarea.selectionStart, textarea.selectionEnd)+
((isDesc)? close : '');
// the text after the selection
var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
// check if there was a selection
if (codetext == open+close) {
//prompt the user
codetext = open+((isDesc)? prompt("Please enter the text you'd like to "+desc, "")+close : '');
}
// update the text field
textarea.value = pretext+codetext+posttext;
}
// set the focus on the text field
textarea.focus();
}
//////////////////////////////////////////////////////////
// inserts an image by prompting the user for the url
BBCode.prototype.insertImage = function (html) {
if(this.noForm()) return false;
var src = prompt('Please enter the url', 'http://');
this.insertCode('img='+src);
}
/////////////////////////////////////////////////////////////////
// inserts a link by prompting the user for a url
BBCode.prototype.insertLink = function (html) {
if(this.noForm()) return false;
this.insertCode('url='+prompt("Please enter the url", "http://"), 'as text of the link', 'url')
}
</script>
<script type="text/javascript">
var bb = new BBCode();
</script>
<input type="button" onclick="bb.insertCode('B', 'bold');" value="B" title="Bold text" />
<input type="button" onclick="bb.insertCode('I', 'make italic');" value="I" title="Italic text" />
<input type="button" onclick="bb.insertCode('U', 'underline');" value="U" title="Underlined text" />
<input type="button" onclick="bb.insertImage();" value="Image" title="Inset an image" />
<input type="button" onclick="bb.insertLink();" value="Link" title="Insert a link" /><br />
<textarea id="ttt" name="ttt" rows="20"></textarea>
<script type="text/javascript">
bb.init('ttt');
</script>
</body>
</html>