Online Documentation Server
 ÏÎÈÑÊ
ods.com.ua Web
 ÊÀÒÅÃÎÐÈÈ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ÏÎÄÏÈÑÊÀ

 Î ÊÎÏÈÐÀÉÒÀÕ
Âñÿ ïðåäîñòàâëåííàÿ íà ýòîì ñåðâåðå èíôîðìàöèÿ ñîáðàíà íàìè èç ðàçíûõ èñòî÷íèêîâ. Åñëè Âàì êàæåòñÿ, ÷òî ïóáëèêàöèÿ êàêèõ-òî äîêóìåíòîâ íàðóøàåò ÷üè-ëèáî àâòîðñêèå ïðàâà, ñîîáùèòå íàì îá ýòîì.




Previous Table of Contents Next

getNewLine()

function getNewLine() {
 var agent = navigator.userAgent

 // if platform is Windows
 if (agent.indexOf("Win") >= 0)
  return "\r\n"
 else
  // if platform is Macintosh
  if (agent.indexOf("Mac") >= 0)
   return "\r"

 // if platform is not Windows or Macintosh
   return "\n"
}

This function returns the characters required to create a new line in a fixed text (such as text in a field or a text area). Since we haven’t discussed the navigator object yet, you can ignore the content of this function for a while. To use this function, assign its returned value to a variable and then use the variable to print a new line in the originating window’s text area:

// variable to hold string to create new line in text field
var nl = getNewLine()

getInput(func, form)

function getInput(func, form) {
 // current value of text area in source window
 var text = form.body.value

 // determine current mode
 if (document.forms[0].mode[0].checked)
  var mode = 0 // simple
 else
  var mode = 1 // prompt

 if (func == 1)
  text += outline(mode)
 if (func == 2)
  text += vertical(mode)
 if (func == 3)
  text += font(mode)
 if (func == 4)
  text += list(mode)
 if (func == 5)
  text += link(mode)

 // update text area in source document
 form.body.value = text
}

The first parameter, func, receives the index of the Write button that the user clicked, whereas the second parameter receives a reference to the form in the originating document. The function first copied the value of the text area to a local variable and, at the end of the function, copied the value of the variable (which changed throughout the function) back to the text area.

Two values determine the function’s actions:

  • the editing mode (prompt or simple)
  • the Write button clicked by the user

The local variable mode is set to 0 if the editing mode is “simple,” and to 1 if the mode is “prompt.”

The script in this document consists of a function for each select object (Write button). The first select object, for example, is associated with the outline() function, the second one with the vertical() function, and so on. The value returned by the function is concatenated to the value of the text variable. Since all five functions associated with the document’s select objects have the same structure, we analyze only one of them.

vertical()

function vertical(mode) {
 var ind = document.forms[0].select2.selectedIndex
 if (ind == 0)
  return ""
   if (ind == 1)
  return "<BR>" + nl
   if (ind == 2)
  if (mode == 0)
 return "<HR>" + nl
 else {
 var prompt1 = prompt("Enter width:", "100%")
 prompt1 = (prompt1) ? " WIDTH=" + prompt1 : ""
 var prompt2 = prompt("Enter size:", "2")
 prompt2 = (prompt2) ? " SIZE=" + prompt2 : ""
 return "<HR" + prompt1 + prompt2 + ">" + nl
  }
   if (ind == 3)
  return "<P>" + nl
}

This function is called by the getInput() function, when the user clicks the second Write button. For each selected index, we create a set of statements that constitute the definition of the corresponding HTML tag. Selecting a category, such as “Page Outline” or “Format,” yields a 0 index for which no HTML tag is generated (an empty string is returned).

Since the <BR> tag does not receive any attributes, the function returns "<BR>" plus a new line (see Figure 27-11). The second option, <HR>, accepts two attributes, WIDTH and SIZE. If the current mode is “simple,” the function returns "<HR>" and a new line, just like the <BR> tag (first option). If the editing mode is “prompt,” the function prompts the user for the values of these attributes. The following script segment prompts the user for the attributes of the <HR> tag when the mode is “prompt”:

var prompt1 = prompt("Enter width:", "100%")
prompt1 = (prompt1) ? " WIDTH=" + prompt1 : ""
var prompt2 = prompt("Enter size:", "2")
prompt2 = (prompt2) ? " SIZE=" + prompt2 : ""
return "<HR" + prompt1 + prompt2 + ">" + nl

The value returned by the first prompt dialog box is assigned to prompt1, the value of the second prompt box is assigned to prompt2, and so on (if there are more than two). If the user clicks the Cancel button, the value of that variable (prompt1, prompt2, etc.) evaluates to false (because null automatically becomes false in a Boolean condition), and an empty string is returned. Otherwise, a string in the form of ATTRIBUTE="value" is returned. The same structures and algorithms are used for the other HTML tags supported by our editor.


Figure 27-11.  The HTML editor with the second select object pulled down.

Take your time to play around with the HTML editor. The script will become much more understandable then.

Summary

A good understanding of JavaScript’s object model is required in order to script windows properly and easily. This chapter serves as a complete reference for writing a JavaScript code related to windows. We have attempted to list as many window-handling applications as possible, for two reasons:

  • A better understanding of the concepts presented in this chapter
  • So you don’t have to work hard when you need one of these scripts

In the following chapters we will focus on built-in objects and new features in Netscape Navigator 3.0x and 4.0.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us