Javascript Notes - Web Dev. - Neils Resource Web |
Summary of Content |
This section is a collection of miscellaneous notes regarding Javascripting. I've needed to use these on more than one occasion, which is why they are here....Creating line breaks in long strings, Calling scripts externally, Running multiple scripts in a single page, Opening Windows with javascript, Standard pop up boxes, Formatting text in javascript..... |
Creating Line Breaks In JavaScript Strings |
JavaScript
provides two ways to create line breaks within a string. The
first is the newline character (
\n ). The
newline character creates line breaks within the output of a
string, whether it is text or JavaScript-generated HTML. Here is a long string without line breaks: var noBreaks = Hello. My name is Neil. What is your name? The same string with line breaks: Hello. My name is Neil. What is your name? |
Opening A Window With JavaScript |
The JavaScript command used to open a window is: window.open For this to work, though, it requires two extra things. Firstly you will need to have some extra information so that the JavaScript knows how to open the window: window.open('link.html','mywindow'); This means that a window called 'mywindow' will open with the page link.html in it, exactly like with the HTML code above. This code can either used as part of your JavaScript code (for example if you included it in the JavaScript code in the <head> section of your page it would open when the page loaded) or can be used when a link is clicked. To do this you must use another JavaScript command called onclick. I will give you more information about how this command works in a later part but for now all you really need to know is the following: In your standard HTML code include a link as follows: <a href="#" onClick="window.open('link.html','mywindow');">Click Here</a> As you can see this is just the same window.open command inside an HTML tag. Manipulating The WindowThe main reason of using JavaScript to manipulate windows, though, is because you can set many things on the window which you could never do with HTML. JavaScript allows you to use commands to decide which parts of the browser window appear. This is done using a third part of the window.open command. This is where you decide the window features:window.open('link.html','mywindow','window features'); There are many things you can include here. For example if you wanted a window that only has a location bar and a status bar (the part at the bottom of the browser) then you would use the following code: window.open('link.html','mywindow','location, status'); The following can be included in the new window command:
Examples Of Manipulating WindowsYou may be a little confused by all these options so I will show you a few examples of opening a window in JavaScript:This window will open with a location bar, toolbar and will be resizable: window.open('window1.htm','the_first_window','location, toolbar, resizable'); This will open another page in this window: window.open('window2.htm','thefirstwindow'); This will open a window 200 pixels wide and 300 pixels high. It is not resizable and has a status bar and will scroll if necessary. This is a very commonly used combination: window.open('window1.htm','thesecondwindow','height=300,width=200,status,scrollbars');
Extract from Freewebmasterhelp.com |
External Javascripts |
You can also include the javascript as an external js file. Take the script and paste it, all by itself, into a word processor. It should be the only thing on the page. Take out the beginning and end SCRIPT commands.
You have the JavaScript file saved. To call for it in from another document insert
these commands: |
Adding more than one JavaScript to a page |
How to add multiple scripts to one page. Many people complain that, while a script works as it
should alone on the page, when a second (or more) one is added, all of a sudden either one or both of the scripts no longer
work. |
Standard Pop Up Windows |
Here are the three different 'built in' popup windows. Alert, Confirm, and Prompt. Alert is used for letting the user know something. Confirm is used when you want to ask the user a yes or no question. The user will be able to click either 'OK' or 'Cancel', OK will return 'true', and Cancel will return 'false'. The last, Prompt, will let the user fill in a small text field and then click OK to send the value of the text field to the script |
Alert |
....</head> |
Confirmation |
...</head> |
Prompt |
...</head> |
|
Last Update: 04/03/2008 - Javascript Miscellaneous Notes - Neils Resource Web