WML WMLS More acronyms than you can shake

  • Slides: 17
Download presentation
WML, WMLS More acronyms than you can shake a stick at!

WML, WMLS More acronyms than you can shake a stick at!

Overview n n Fibonacci re-visited WML More samples Templates, access control, and comments n

Overview n n Fibonacci re-visited WML More samples Templates, access control, and comments n WMLS Initialisation control and strings

The Fibonacci Sequence n The Fibonacci Sequence is a sequence of numbers where each

The Fibonacci Sequence n The Fibonacci Sequence is a sequence of numbers where each number is the sum of the previous two. The Fibonacci Sequence was originally devised by a fellow named Fibonacci (surprise) in the 1600’s to model the population growth from a pair of immortal rabbits. His question was, if every month every pairing of rabbits produced one more rabbit, could he predict how many rabbits he would have after a certain number of months? • This research is of key interest to rabbits and French chefs everywhere. We’ll write Fib(n) for “The number of Fibonacci rabbits in month n”.

The Fibonacci Sequence In the first two months, Fib collected rabits: Fib(1) = 1

The Fibonacci Sequence In the first two months, Fib collected rabits: Fib(1) = 1 Fib(2) = 1 During month three he introduced the two rabbits: Fib(3) = 2 = 1 + 1 In month four, the two produced one more rabbit: Fib(4) = 3 = 2 + 1 In month five, the three produced two more: Fib(5) = 5 = 3 + 2 In month six, the five produced three more: Fib(6) = 8 = 5 + 3

The Fibonacci Sequence n In general, each month, Fib(month) is equal to Fib(the previous

The Fibonacci Sequence n In general, each month, Fib(month) is equal to Fib(the previous month) plus Fib(the month before that): Fib(1) = 1 Fib(2) = 1 Fib(n) = Fib(n-1) + Fib(n-2) n How would you translate this into code? (Rhetorical question. Answer it in the homework, not now. )

WML - Combining approaches n Let’s look at combining some of what we’ve seen.

WML - Combining approaches n Let’s look at combining some of what we’ve seen. How would you write a page that let the user choose an image? n We’ll need: An <img> tag to display the picture; <select> and <option> tags to pick; some <p>’s to contain the content; one <card> to hold them all, and in the <wml> bind them.

WML <? xml version="1. 0"? > <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1. 3//EN" "http:

WML <? xml version="1. 0"? > <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1. 3//EN" "http: //www. wapforum. org/DTD/wml 13. dtd"> <wml> <card id="main" title="How's the Weather? "> <p>Today's weather : <img src="$(name)" alt="$(name)" /></p> <p> <select name="name"> <option value="sunny. wbmp" onpick="#Main">Sunny</option> <option value="partcldy. wbmp" onpick="#Main">Partly cloudy</option> <option value="cloudy. wbmp" onpick="#Main">Cloudy</option> <option value="rainy. wbmp" onpick="#Main">Rainy</option> </select> </p> </card> </wml>

WML n How did the user’s choice wind up being shown? Each time the

WML n How did the user’s choice wind up being shown? Each time the user clicked an <option> tag, that <option> had an onpick event handler. The onpick reloaded the page. Because each <option> also had a value attribute, that value was assigned to the <select> tag’s variable. The <select> tag’s variable, $(name), was the file path we used for the <img> tag. So each time the user clicked, it changed the value of $(name) and refreshed the card; which changed the value used in the src attribute of the <img> tag; which changed the picture that was being displayed. An interesting note is that the <select> tag picks its value from the first <option> tag, so we get an image showing up on first load.

WML n The <onevent> tag meets the <option> tag: You can add an <onevent>

WML n The <onevent> tag meets the <option> tag: You can add an <onevent> tag to an <option> tag to enhance the onpick behavior: <select name="name"> <option value="sunny. wbmp"> <onevent type=“onpick”> <refresh> <setvar name=“var 1” value=“val 1” /> <setvar name=“var 2” value=“val 2” /> <setvar name=“var 3” value=“val 3” /> </refresh> </onevent> Sunny </option>. . . n This is an effective way of dealing with the fact that the onpick attribute only takes a URL.

WML - Access control n It’s always possible to type in a URL manually.

WML - Access control n It’s always possible to type in a URL manually. Wouldn’t it suck if your e-banking web site had a password-check page that forwarded the user to the “transfer your money” page, and the user could just type in the URL for the money-transfer page without checking their password? n <access> and <head> The <head> tag contains info about a document. The <access> tag belongs in a document’s <head> tag. The <access> tag limits the domain and filepath that the user can come from to get to this tag: (from the documentation at phone. com: ) • domain - The URL domain of other decks that can access cards in this deck. The default value is the domain of the current deck. • path - The URL root of other decks that can access cards in the deck. The default value is "/" (the root path of the current deck) which lets any deck within the specified domain access this deck.

WML - Access control n Access-control example: <wml> <head> <access domain=”luton. ac. uk” path=”/”

WML - Access control n Access-control example: <wml> <head> <access domain=”luton. ac. uk” path=”/” /> </head> <card>. . . n This would define a document which could only be reached by clicking links in documents downloaded from Luton’s web server; you couldn’t link to the document from anyplace else.

WML - Comments n In WMLScript, comments can be either // single-line comments ,

WML - Comments n In WMLScript, comments can be either // single-line comments , or /* multi-line comments */ exactly like C++ or Java. There’s no Java. Doc(tm) in WML. n To write comments in WML, use the <!-- and --> markers.

WML Script for initialisation n Last week we saw that you could initialise the

WML Script for initialisation n Last week we saw that you could initialise the value of a variable with the onenterforward event: <onevent type="onenterforward"> <refresh> <setvar name=”Foo” value=”Bar"/> </refresh> </onevent> This can be problematic if you want to change the value of $(Foo) in another card and then the user clicks forward to the first card again (following a link forward, as opposed to hitting the Prev button to go back. )

WML Script for initialisation n An example of things going wrong: <card id="card 1"

WML Script for initialisation n An example of things going wrong: <card id="card 1" title="Card #1"> <onevent type="onenterforward"> <refresh> <setvar name="my. Var" value="first card" /> </refresh> </onevent> <p> my. Var = $(my. Var) </p> <p> Click for <a href="#card 2">second card</a> </p> </card> <card id="card 2" title="Card #2"> <onevent type="onenterforward"> <refresh> <setvar name="my. Var" value="second card" /> </refresh> </onevent> <p> my. Var = $(my. Var) </p> <p> Click for <a href="#card 1">first card</a> </p> </card> Here the value of my. Var will be changed every time the phone loads card 1 or card 2. That’s not a bug; it’s a feature. But what if you wanted to preserve the change you made in card 2, but still have a valid value coming into card 1?

WML Script for initialisation n One way to fix things would be to add

WML Script for initialisation n One way to fix things would be to add an ‘initialisation’ card at the front of the document: <card id="card 0"> <onevent type="onenterforward"> <go href=“#card 1”> <setvar name="my. Var" value="first card" /> </refresh> </onevent> </card> <card id="card 1" title="Card #1"> <p> my. Var = $(my. Var) </p> <p> Click for <a href="#card 2">second card</a> </p> </card> <card id="card 2" . . . Here the value of my. Var will be changed every time the phone loads card 2, but only once before it loads card 1. So the change made in card 2 is preserved.

WML Script for initialisation n Another solution to the same problem would be to

WML Script for initialisation n Another solution to the same problem would be to call a WML Script function: <wml> <card id="card 1" title="Card #1"> <onevent type="onenterforward"> <go href="Things. Go. Right. wmls#init()" /> </onevent> <p> my. Var =. . . . n The WML Script can actually test the var: extern function init() { if (!String. compare(WMLBrowser. get. Var("my. Var"), "")) WMLBrowser. set. Var("my. Var", "first card (from WMLS)"); WMLBrowser. refresh(); } n This way the var is only initialised once.

WML Script - Strings n A few words about strings in WML Script: A

WML Script - Strings n A few words about strings in WML Script: A “String” is a sequence of characters--letters and numbers-like “bob the string” or “number : 1234” or “!@#%!%”. The WMLS String Library provides a host of functions to manipulate Strings. n String. compare(S 1, S 2) : Test for equality compare(s 1, s 2) will actually test whether two strings are different. So if (compare(s 1, s 2)). . . is testing whether s 1 != s 2. To test whether s 1 equals s 2, use !compare(s 1, s 2). Remember that comparison is case-sensitive!