JavaScript

Posts about the client side scripting language JavaScript.

Highlight The Contents Of A Textarea With JavaScript

If you want to give your users a little snippet of code it is nice to give them the option of selecting the entire block of code without having to highlight the text manually. This can be done with a simple JavaScript button.

Take the following form:

philipnorton42 Wed, 08/27/2008 - 08:47

JavaScript Crazy Window Zoomer

26th August 2008

I really don't know how this would be useful, but it might teach you a couple of things about how to use the browser window functions and properties.

The following function resizes the browser window to nothing and then gradually increases this to full screen in a series of steps, at each step the window is moved so that it is in the middle of the screen. This in effect makes it look like the browser window is zooming in.

Format Numbers With Commas In JavaScript

31st July 2008

I found a good function that details how to format numbers with commas in JavaScript and thought I would reproduce it here. It basically takes any number and turns it into formatted string with the thousands separated by commas. The number_format() function is available in PHP and I have been relying on PHP to sort out the formatting before returning it via an AJAX call. I can now include this function into my JavaScript library and do this on the client side.

Adding Numbers In JavaScript

28th July 2008

You think I'm joking right? Well, due to a silly mistake (in my opinion) when creating the language the concatenation character is the same as the plus symbol. This means that sometimes JavaScript will add them together and sometimes it will concatenate them.

This occurs if JavaScript encounters any part of the calculation to be a string. If it is then it will concatenate the whole expression. For example.

Randomise JavaScript Array

4th July 2008

Randomising a JavaScript array can be done in one or two ways. The easy way is to create a function that returns a random number and then use the sort() function of the Array object to sort the array by a random value.

Merge Two JavaScript Arrays

3rd July 2008

Here is a simple bit of code that you can use to merge one or more arrays. The function you need is called contact().

Take the following two arrays, both of which contain numbers.

  1. var array1 = new Array(1,2,3,4);
  2. var array2 = new Array(5,6,7,8);

To join these two arrays together we use the concat() function like this.

Loading Page Styles And JavaScript With JavaScript

2nd July 2008

One good technique when using JavaScript is to load a single JavaScript file and get this file to load any other JavaScript or CSS documents that are needed. This means that you can simplify the instillation of a script on a page by including a single file, which then loads everything else it needs. Here is how to accomplish such a task.

Enabling Tabbing In A Textarea

20th June 2008

When a user presses the tab key in a browser window the normal action is for the user to move the focus to a different control. To enable a word processor like tab effect in a text area you need to catch the keystroke and add in the tab character to where ever the cursor is. This is the main issue with creating this solution, it is easy to add a tab to the end of the text, but most users might want to add a tab half way through the text.

Take the following HTML text area.

Submitting A HTML Form Using JavaScript

19th June 2008

In order to submit a form using an event you need to run a click event on any submit button in the form. Take the following form.

 

To run a submit event using JavaScript we just find the submit button via the id and run click() against it.

JavaScript To Stop Frames

27th May 2008

The following section of JavaScript will detect if anyone is viewing your page in a frame. If they are then the code will redirect them to your site. It essentially stops people using frames to poach your content and stops online proxy software from viewing your site.