Free Android CakePHP GMaps Articles by Bali Web Design

July 21, 2010

Sleep or wait process on javascript thread

Filed under: html,javascript — admin @ 10:10 pm

Actually there is no Thread.sleep method on javascript that work on Threading. Moreover there is no true threading in javascript. So everybody that want to deal with threading need to emulate it. In this article i would like to show you some method that emulate sleep method for waiting on a process. Each of this method has each own disadvantage, so you need to choose which is suitable for your case.

First method is sleep using javascript loop. This is first method that come to my mind when emulate sleep method on javascript. We wait for the process by checking the time in the loop.

function wait(millisecond) {
var date = new Date();
var current_date = null;

do { current_date = new Date(); }
while(current_date-date < millisecond)
}

// do something
wait(1000); // wait 1000 millisecond
// do something

(more…)

How to handle user closing browser windows or page using javascript

Filed under: html,javascript — admin @ 12:40 am

Sometime we need to handle or do something when user closing a browser windows or page. Maybe we want to ask user if they really want to close the page or it was a mistake click. This procedure all can be done using javascript.

There are two condition or event that you can intercept before it really happen, first when user close the browser windows and second is when browser windows will be closed without any way to abort it.

For first condition we can handle it using window.onbeforeunload event.

function closeHandler() {
return “Are you sure you want to close this page?”;
}

window.onbeforeunload = closeHandler;

(more…)

August 31, 2009

How to print javascript object properties

Filed under: html,javascript — Tags: — admin @ 2:32 am

Sometime we need to print the structure data that return by a function. I was work on a task for google map and need to print object properties of Placemarks object on javascript. I need to analyze what kind of structure data return by geocoding function. The code below is a short function to print object properties in javascript

function isObject(obj) {
return obj.constructor == Object;
}

function print_object(object, level){
var tab = “”;
if(level > 0){
for(i = 0; i < level; i++) tab += “\t”;
}
var str = “”;
for(prop in object) {
if(!isObject(object[prop])) str += tab + prop + ” value :” + object[prop] + “\n”;
else str += tab + prop + “\n” + print_object(object[prop], level + 1);
}
return str;
}

and how to use this function is really simple approach

address = addresses.Placemark[0];
alert(print_object(address, 0));

in this scenario, i would like to print first placemark object return by geocoding function. Parameter level is use to make nice print view using tab character.

September 20, 2008

How to load javascript file on window on load

Filed under: html,javascript — Tags: , — admin @ 7:36 pm

Here is another way to load a javascript file on window on load.

<script type="text/javascript">
window.onload = function () {
var script = document.createElement("script");
script.src = "site/js/3000297746.js";
document.body.appendChild(script);
};
</script>

July 29, 2008

How to post form into a popup window

Filed under: html,javascript — Tags: , — admin @ 10:04 am

When you need to send a variable to a popup window from mainpage, ussually we will put it in url as GET method. But using this problem we will get problem when the variable has long character. We know that GET has limited long.

So we need to use POST method to submit a form. But how we can do it? I have a trick how to do it.

First we create a javascript for handling onsubmit event on html form.

function thePopupWindows(windowsname){
var win = window.open('', windowsname, ''width=580,height=450');
return true;
}

and then set this function on HTML Form :

<form action="popupwin.php" method="post" target="thepopup" onsubmit="return thePopupWindows(this.target);">
<input id="name" name="name" type="text" />
<input type="submit" value="Submit">
</form>