JavaScript Exercises 3

Windows

  1. Positioning a window Investigate the window.moveTo() method and the properties available for the screen object. Experiment with these methods and properties and then use this knowledge to change indWin.html so that the help window is located at the top of the screen in the centre. Hint: You will need to calculate the x co-ordinate for the window as (width of the screen / 2) - (width of help window / 2)
  2. Repeatedly opening a window irritating.html contains code that will display an advert in a pop-up window when the page is loaded. Make the page even more irritating by adding code so that it checks every 3 seconds that the pop-up window is still open and if it isn't opens it and makes sure it is on top. Do you think there are ever occasions when doing something like this is a good idea?
  3. Converting an alert box to a window

    windowQuiz.html is a simple quiz. After the user has answered each question it uses an alert box to tell them whether their answer is correct or not and reports their current score. Study the code and try to understand it. Replace the alert box with a window that is opened when the user first clicks "check answer". The window should tell the user whether or not they got the question correct and display their current score. It should be updated each time the user clicks "check answer". Make the window display a happy face if the answer is correct and a sad face if it isn't.
  4. Study cookie.html and make sure that you understand how it works. Add code to it so that it also asks for the user's favourite colour and stores it as a cookie. When the user returns to the page it should use the cookie to set the background colour for the page (document.bgColor). Make sure that the colour cookie gets deleted at the same time as the uname cookie.
  5. Currently in cookieTrolley.html the contents of the users shopping trolley are preserved until the user exits from the browser. Change the code so that the shopping trolley contents are preserved for ten minutes after the user has last added something to the trolley. Test that it works. Note - you can do this by getting the date object (see cookie.html), using one of the date methods to add ten minutes to the time (see the online JavaScript documentation) and then setting the expiry attribute of the cookie. What are the advantages and disadvantages of explicitly setting the cookie's expiry time like this?
  6. cookieTrolley.html has a function called removefromTrolley() which is intended to allow the user to delete items from the trolley. Write the code for this function. Remember that deleting an item from the trolley isn't the same as deleting the whole cookie.
  7. Cookies can only be accessed by the server from which they were set, or at least one in the same domain. How then can they be used by ad companies such as "doubleclick" to track your use of other websites? Visit www.cookiecentral.com and find the answer to this question. Feeling paranoid about cookies? Try installing Spybot - Search and Destroy.

best viewed using Mozilla browsers
© k.mcmanus 2004
Valid XHTML 1.1!. Valid CSS. WCAG priority 3 approved