Saturday, January 7, 2012

Javascript exercise: push/pop of array

  • push():
    adds new elements to the end of an array, and returns the new length.
  • pop():
    removes the last element of an array, and returns that element.


Example:
Javascript exercise: push/pop of array
<!doctype html>
<head>
<title>Mobile-Web-App: push/pop of array in Javascript</title>
<meta charset="utf-8">

<script>
function myOnLoad(){

var arrayinfo;

var array = new Array();
array.push("Sunday");
array.push("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

arrayinfo = "length of array: " + array.length + "<" + "BR/" + ">";

while(array.length>0){
arrayinfo += array.pop() + "<" + "BR/" + ">";
}

elementInfo = document.getElementById("info");
elementInfo.innerHTML = arrayinfo;

}

window.onload = myOnLoad;

</script>


</head>
<body>
<h1>Javascript exercise: push/pop of array</h1>

<p id="info"></p>

</body>
</html>

Friday, January 6, 2012

Javascript code sample: Get all elements with ID

Call function document.getElementsByTagName('*') using widecard '*' to retrieve all element with ID.

Example:
Javascript code sample: Get all elements with ID


<!doctype html>
<head>
<title>Mobile-Web-App: Test Javascript</title>
<meta charset="utf-8">

<script>
function myOnLoad(){

var allElements = new Array();
var result = "=== all elements with ID : ===" + "<" + "br/" + ">";;
allElements = document.getElementsByTagName('*');


for (var i=0; i < allElements.length; i++)
{
if (allElements[i].id)
{
result = result + allElements[i].id + " " + "<" + "br/" + ">";;
}
}

var arrMyName = new Array();
elementResult = document.getElementById("result");
elementResult.innerHTML = result;


}

window.onload = myOnLoad;

</script>


</head>
<body>
<h1>Mobile-Web-App: Javascript Exercise</h1>
<p id="title"><b>Get All elements with ID</b></p>

<p id="msg" name="myname">Hello JavaScript!</p>
<p id="myBlog" name="myname">Mobile-Web-App</p>
<div id="myURL" name="myname">
<p id="innerP">Inner p</p>
<a href="http://mobile-web-app.blogspot.com/">http://mobile-web-app.blogspot.com/</a>
</div>

<p>No ID</p>

<p id="result"></p>

</body>
</html>


Get array of HTML elements with specified name - getElementsByName

The function document.getElementsByName(n) return a array of elements with name of n.

Example:
<!doctype html>
<head>
<title>Mobile-Web-App: Test Javascript</title>
<meta charset="utf-8">

<script>
function myOnLoad(){

var arrMyName = new Array();
arrMyName = document.getElementsByName("myname");

for(var i = 0; i < arrMyName.length; i++)
{
var obj = arrMyName.item(i);

obj.style.color = "#00ff00";
obj.innerHTML = "It's the item: " + i + " : " + obj.innerHTML;

}

alert("There are " + arrMyName.length + " elements with name of 'myname'.");

}

window.onload = myOnLoad;

</script>


</head>
<body>
<h1>Mobile-Web-App: Test Javascript</h1>
<p>How to access HTML elements from JavaScript, using document.getElementById().</p>

<p id="msg" name="myname">Hello JavaScript!</p>
<p name="myname">Mobile-Web-App</p>
<div name="myname"><a href="http://mobile-web-app.blogspot.com/">http://mobile-web-app.blogspot.com/</a></div>



</body>
</html>

getElementsByName

Access HTML elements from JavaScript, using document.getElementById().

To access HTML elements with ID from JavaScript, we can use document.getElementById(). It's a example to change the text in HTML.
Change html element's text
<!doctype html>
<head>
<title>Mobile-Web-App: Test Javascript</title>
<meta charset="utf-8">

<script>
function myOnLoad(){
var myMsg = document.getElementById("msg");
msg.innerHTML = "Nice to me you:)";
}

window.onload = myOnLoad;

</script>


</head>
<body>
<h1>Mobile-Web-App: Test Javascript</h1>
<p>How to access HTML elements from JavaScript, using document.getElementById().</p>

<p id="msg">Hello JavaScript!</p>




</body>
</html>


Remark:
- If we place JavaScript code in <head>, it will be executed before the page loaded. So it cannot find the id. That's why we implement our job in a function myOnLoad(), and call the function once page load finished. The code "window.onload = myOnLoad" means once the document loaded, call the function with name of myOnLoad.

- To change the text in HTML document, we can set innerHTML property of the element.



Thursday, January 5, 2012

NetBeans IDE 7.1 Overview


Find out what is new and cool in the latest NetBeans 7.1 release. The NetBeans IDE is an integrated development environment for software developers. It provides the tools you need to create professional desktop, enterprise, web, and mobile applications with the Java language, as well as PHP, JavaScript, Groovy and Grails, and C/C++.

NetBeans IDE introduces support for JavaFX 2.0 by enabling the full compile/debug/profile development cycle for JavaFX 2.0 applications. The release also provides significant Swing GUI Builder enhancements, CSS3 support, and tools for visual debugging of Swing and JavaFX user interfaces. Additional highlights include Git support integrated into the IDE, new PHP debugging features, various JavaEE and Maven improvements, and more.

http://netbeans.org/

Wednesday, January 4, 2012

Where to place Javascript in HTML web page

Javascript can be placed in <head> or <body>. It can be Javascript code in HTML file, in-between <script> and </script>. Or link to external javascript file using src attribute.

  • Place Javascript code in <head>. It will be executed as long as the browser parses the head; before the page body loaded.
    <!doctype html>
    <head>
    <title>Mobile-Web-App: Test Javascript</title>
    <meta charset="utf-8">

    <!-- Place Javascript code in <head> -->

    <!-- Place Javascript code here directly -->
    <script>
    alert("Hello All:) - It's Javascript in <head>.");
    </script>

    <!-- Link to external Javascript -->
    <script src="extJavascript.js">
    </script>

    </head>
    <body>
    <h1>Mobile-Web-App: Test Javascript</h1>
    <p>Where to add Javascript in HTML: in <head></p>

    </body>
    </html>

    Place Javascript in head
    Place Javascript in head, link to external .js file

  • Place Javascript code in <body>.
    <!doctype html>
    <head>
    <title>Mobile-Web-App: Test Javascript</title>
    <meta charset="utf-8">
    </head>
    <body>
    <h1>Mobile-Web-App: Test Javascript</h1>
    <p>Where to add Javascript in HTML: in <body></p>

    <!-- Place Javascript code in <body> -->

    <!-- Place Javascript code here directly -->
    <script>
    alert("Hello All:) - It's Javascript in <body>.");
    </script>

    <!-- Link to external Javascript -->
    <script src="extJavascript.js">
    </script>

    </body>
    </html>

    Place Javascript in body


extJavascript.js
alert("Alert from External Javascript");




Head First HTML5 Programming: Building Web Apps with JavaScript


HTML has been on a wild ride. Sure, HTML started as a mere markup language, but more recently HTML’s put on some major muscle. Now we’ve got a language tuned for building web applications with Web storage, 2D drawing, offline support, sockets and threads, and more. And to speak this language you’ve got to go beyond HTML5 markup and into the world of the DOM, events, and JavaScript APIs.

Now you probably already know all about HTML markup (otherwise known as structure) and you know all aboutCSS style (presentation), but what you’ve been missing is JavaScript (behavior). If all you know about are structure and presentation, you can create some great looking pages, but they’re still just pages. When you add behavior with JavaScript, you can create an interactive experience; even better, you can create full blown web applications.

Head First HTML5 Programming is your ultimate tour guide to creating web applications with HTML5 and JavaScript, and we give you everything you need to know to build them, including: how to add interactivity to your pages, how to communicate with the world of Web services, and how to use the great new APIs being developed for HTML5.

Here are just some of the things you’ll learn in Head First HTML5 Programing:

  • Learn how to make your pages truly interactive by using the power of the DOM.
  • Finally understand how JavaScript works and take yourself from novice to well-informed in just a few chapters.
  • Learn how JavaScript APIs fit into the HTML5 ecosystem, and how to use any API in your web pages.
  • Use the Geolocation API to know where your users are.
  • Bring out your inner artist with Canvas, HTML5’s new 2D drawing surface.
  • Go beyond just plugging a video into your pages, and create custom video experiences.
  • Learn the secret to grabbing five megabytes of storage in every user’s browser.
  • Improve your page’s responsiveness and performance with Web workers.
  • And much more.