Replace all occurrences of a string using javascript

In JavaScript we use “replace” function to replace a particular word or string.

For Example,

var str = "Replace text in javascript using replace function of javascript";
str = str.replace('javascript','jscript');

here, it will only replace the first occurrence of the ‘javascript’ in string.
Output: “Replace text in jscript using replace function of javascript”

So, to replace all occurrences use

str  = str.replace(/javascript/g, 'jscript');
str  = str.replace(new RegExp('javascript','g') , 'jscript');

Open popup window in center position

To open popup window in center position of screen. Following function is useful.

function OpenWin()
var width = 300; //width of popup window
var height = 500; //height of popup window
var left = parseInt(((screen.width - width) / 2));
var top = parseInt(((screen.height - height) / 2));

var win ='url','name','width=' + width + 'px,height=' + height + 'px,left=' + left + 'px,top=' + top + 'px');

Variable declaration

Hello all,

Today, I am writing some common naming convention for declaring variables.

Camel Notation : The first letter is lowercase and each appended word begins with an uppercase letter. For e.g.: var myTestVar = 0

Pascal Notation : The first letter is uppercase and each appended word begins with an uppercase letter. For e.g.: var MyTestVar = 0

Hungarian Type Notation : Pretends a lowercase letter to the beginning of a Pascal Notation variable name to indicate the type of the variable. For e..g.: var iMyTestVar = 0 where i for integer.

Set Scroll Position of DataGrid/GridView

JavaScript to set the scroll position of DataGrid.

<script language = "javascript">

///This function sets the scroll position of div to cookie.
function setScrollPos()
var divY = document.getElementById('div1').scrollTop;
document.cookie = "divPos=!*" + divY + "*!";

///Attaching a function on window.onload event.
window.onload = function()
var strCook = document.cookie;
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS+2,intE);
document.body.scrollTop = strPos;

/// This condition will set scroll position od <div>.
var intdS = strCook.indexOf("!*");
var intdE = strCook.indexOf("*!");
var strdPos = strCook.substring(intdS+2,intdE);
document.getElementById('div1').scrollTop = strdPos;

/// Function to set Scroll position of page before postback.
function SetScrollPosition()
var intY = document.body.scrollTop;
document.cookie = "yPos=!~" + intY + "~!";

/// Attaching   SetScrollPosition() function to window.onscroll event.
window.onscroll = SetScrollPosition;

In DIV Tag add the onscroll=”setScrollPos();”.

For Example

<DIV id="div1" style="OVERFLOW: auto; WIDTH: 100%; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none;
BORDER-LEFT-STYLE: none; HEIGHT: 200px; BORDER-BOTTOM-STYLE: none" onscroll="setScrollPos();">

Place DataGrid here...


Watermark Textbox

This is the simple way to make input text box as a watermark text box.
I have put onblur and onfocus event in textbox. See below example.

Search :  <input type="text" onFocus="if (this.value == 'Movies, Actor, Director'){this.value='';'Black';'normal';}"
onblur="if (this.value == '') {'gray';this.value='Movies, Actor, Director';'normal';} " value='Movies, Actor, Director' style="color:gray;font-style: normal; " />


QueryString in JavaScript

JScript function to retrive QueryString from URL. Function is written for One Parameter.

function retVal(qstr)
take last location of ?
find first location of qstr +2
retrieve value before next &

var qURL = new String(window.location);
var qMark= qURL.lastIndexOf('?');
var Lenqstr = qstr.length;
//retrieve location of qstr

var Start = qURL.indexOf('?' + qstr + '=')

if (Start==-1)
Start = qURL.indexOf('&' + qstr + '=')

if (Start==-1)
return 0; //not found

Start = Start + Lenqstr + 2;
var Temp= qURL.indexOf('&',Start); //next pair start

if (Temp ==-1)
return qURL.slice(Start,Temp );
qURL=null;//set String null

//For Finding param name
//var iURL = new String(window.location);
//var iMark = iURL.lastIndexOf('?');
//var iEqual = iURL.lastIndexOf('=');
//var str = iURL.slice(iMark+1,iEqual);


To Call this page use PageName.aspx?QParam=tValue