Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Friday, March 6, 2009

Javascript back button

Create a link or a button to go to the previous page, using this code:
<a href="javascript:history.go(-1)">Back</a>



This way, the visitor can go back to the previous page without pressing the "Back" button of his browser.

Monday, November 12, 2007

Change text link in Javascript

How to change a link text in Javascript



Let's say you have a link, with the anchor text 'mylink', and you want to change this text using Javascript. For this we will use innerHTML attribute. Here it is:

document.getElementByID( 'mylinkid' ).innerHTML = 'my new link text';

You will probably need this if youn need to show/hide some html elements.


Changing text in a link using Javascript

Saturday, November 10, 2007

Javascript explode function

Javascript function like PHP explode function



Looking for an explode function in Javascript? JS has such a function, called split. Split take a string, and splits it by a separator, then return an array containing the result. So it works just like explode PHP function.

Example:

var mystring = 'test explode split string';
var myarray = mystring.split(" ");

Split array would have 4 elements: test, explode, split, string.

So javascript split function is the equivalent of explode function in PHP.

Thursday, October 18, 2007

Get number of characters in Javascript

How to count number of characters in a string in Javascript

length javascript's function returns the length (number of characters) of a given string.

For example:



var mystring = 'test string';

alert( mystring.length ) ;


This will alert a window with the number of characters of mystring variable.

If you want to get number of characters in a textfield using javascript, that you need to call the same function, length this way:


alert( document.yourform.yourfieldname.value.length );

Tuesday, October 16, 2007

Javascript open centered popup window

How to open a centered pop-up window in Javascript


Here is a Javascript function that allows you to open a centered window, no matter what resolution the visitor uses.

function OpenCenteredWindow( url, width, height )
{
var left = Math.floor( (screen.width - width) / 2);
var top = Math.floor( (screen.height - height) / 2);
var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
winParms += ",toolbar=no,status=no,scrollbars=yes,menubar=no,resizable=no,location=no,directories=no";
var win = window.open(url, '', winParms);
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
return win;
}

There are three parameters:
  • url - the address you want to open in the new window
  • width - the width of the new popup window
  • height - the height of the new centered window
Also you can set what parameters the new window will have: toolbar, status, scrollbars, menubar, resizable, location, directories.

Tuesday, October 9, 2007

Javascript change element visibility

Want to change an element visibility from javascript. Here is an example.

document.getElementById("your_element_id").style.visibility = "hidden";
document.getElementById("your_element_id").style.visibility = "visible";

As you can see the visibility for an element can be hidden or visible. Make sure you have set an ID for your element.