112Part IIJavaScript Tutorialanother, as often happens (Web hosting resellers) in event

112Part IIJavaScript Tutorialanother, as often happens in event handlers. In the following example, the alert()methodrequires a quoted string as a parameter, but the entire method call also must be insidequotes: onclick= alert( Hello, all ) JavaScript imposes no practical limit on the number of characters that a string can hold. However, most older browsers have a limit of 255 characters in length for a script statement. This limit is sometimes exceeded when a script includes a lengthy string that is to becomescripted content in a page. You need to divide such lines into smaller chunks using tech- niques described in a moment. You have two ways to assign a string value to a variable. The simplest is a basic assignmentstatement: var myString = Howdy ; This works perfectly well except in some exceedingly rare instances. Beginning withNavigator 3 and Internet Explorer 4, you can also create a string object using the more formalsyntax that involves the newkeyword and a constructor function (that is, it constructs anew object): var myString = new String( Howdy ); Whichever way you use to initialize a variable with a string, the variable receiving the assign- ment can respond to all Stringobject methods. Joining stringsBringing two strings together as a single string is called concatenatingstrings, a term youlearned in Chapter 6. String concatenation requires one of two JavaScript operators. Even inyour first script in Chapter 3, you saw how the addition operator (+) linked multiple stringstogether to produce the text dynamically written to the loading Web page: document.write( of + navigator.appName + . ); As valuable as that operator is, another operator can be even more scripter friendly. Thisoperator is helpful when you are assembling large strings in a single variable. The strings maybe so long or cumbersome that you need to divide the building process into multiple state- ments. The pieces may be combinations of string literals(strings inside quotes) or variablevalues. The clumsy way to do it (perfectly doable in JavaScript) is to use the addition opera- tor to append more text to the existing chunk: var msg = Four score ; msg = msg + and seven ; msg = msg + years ago, ; But another operator, called the add-by-value operator, offers a handy shortcut. The symbolfor the operator is a plus and equal sign together (+=). This operator means append the stuffon the right of me to the end of the stuff on the left of me. Therefore, the preceding sequenceis shortened as follows: var msg = Four score ; msg += and seven ; msg += years ago, ;

Leave a Reply