milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. JavaScript
  4. JavaScript

onLoad

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 07 November 2013
Last Updated: 25 November 2014
Hits: 5436

Just short note to my self. If getElementById doesn't work, most probably I forgot window.onload, because if page is not loaded yet, and javascript is executed, then my element with ID will not exist. So, html part looks like:

<!DOCTYPE html>
<html>

	<head>
		<script type="text/javascript" src="/arrayAssignment.js"></script>
	</head>
	
	<body>
		Variable <strong>A</strong>: <span id='varA'></span>
		<br/>
		<strong>var b = a</strong>: <span id='b=a'></span>
		<br/>
		b.splice(3);
		<br/>
		Variable <strong>A</strong>: <span id='bSplice'></span>
		<br/>
	</body>

</html>

and javascript part should lok something like:

window.onload = function() {
	var a = [];
	var b = [];
	a = [1, 2, 3, 4]
	b = a;
	document.getElementById("varA").innerHTML = a;
	document.getElementById("b=a").innerHTML = b;
	b.splice(3);
	document.getElementById("bSplice").innerHTML = a;
};

This is just short note to myself since I always forget onLoad part :)

Array in javascript

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 07 November 2013
Last Updated: 07 November 2013
Hits: 5203

It seems that javascript considers array as a pointer, therefore, simple assignment like:

a = b

will not copy values, variable a will just "point out" to variable b.

Consider following example:

var a = [];
var b = [];
a = [1, 2, 3, 4]
b = a;
b.splice(3);

In this case, variable a will be 1, 2, 3

Full example you can see here.

Solution is something like:

b = [].concat(a);

Taken from here.

setTimeout hell

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 14 June 2013
Last Updated: 25 November 2014
Hits: 5448
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <head>
		<title></title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		
		<script>
			function errorTimeOut(strInTest) {
				alert('I will wait 10 seconds, but no variables (error: Uncaught ReferenceError: strTest is not defined). ' + strInTest)
			}
			
			function errorAlert() {
				var strTest = "I am here!";
				setTimeout("errorTimeOut(strTest)", 10000);
			}
			
			function correctTimeOut(strInTest) {
				alert('I was waiting 20 seconds, and everything is ok: ' + strInTest)
			}

			function correctAlert() {
				var strTest = "I am here!";
				setTimeout(function() {
					correctTimeOut(strTest)
				}, 20000);
			}
		
			setTimeout(alert('I am fired immediately'), 10000);
			
			errorAlert();
			
			correctAlert();
			
		</script>
	
	</head>
	
	<body>

	</body>
   
</html>

As you can see from previous code, setTimeout(alert('I am fired immediately'), 10000); will not work.

setTimeout("alert('I will wait')", 10000); - this will work, alert under quotes, but you can not pass varibles.

And the code:

function correctAlert() {
	var strTest = "I am here!";
	setTimeout(function() {
		correctTimeOut(strTest)
	}, 20000);
}

Will work properly.

Create an array of objects

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 26 August 2012
Last Updated: 26 August 2012
Hits: 5024
var tableProperties = {
    aoColumns: []
};

for (var i = 0; i < 9; ++i) {

    var obj = {
        _origColumnPosition: i
    };
    tableProperties.aoColumns.push(obj);
}
  1. Prototype a string
  2. False is not defined
  3. Check members of object
  4. Document.Cookie Property Returns an Empty String

Page 7 of 9

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9