HP’s Blog

If I put alert() message then my javascript works fine otherwise script is not working

Posted by: hspinfo on: January 12, 2009

When you deal with the javascript and when you are using any form elements of the page in the script then you better make sure that your whole page gets load first. The form elements that you are using into the script will only get used until the page loads completely.

I have faced one situation where I was using popup window and was transferring the data of parent window to the opened popup. I have created one function into the popup window which will set some values into the text elements of the popup. I am calling this function from the parent window by passing some arguments to that function.

Now what happen is, when I am calling this function by putting some ‘alert()’ messages into the caller function and called function then the script is working fine for me. And when I am removing the ‘alert()’ message box from the script, the script stop working.

When I have analysis the script and debug it, I found that when I am calling a popup function from my parent window, the function did not set values into the text elements of the popup. This is because the popup window is not loaded completely. When I am putting an ‘alert()’ message box, the popup window gets sufficient time to load while I am busy clicking on the OK button for the alert message.

Thats it… I found that I was writing the script for the form elements which are not yet loaded into the popup and thats why I am getting this issue. I have resolved this issue by calling the javascript function of the popup only after it gets loaded completely.

If you found any similar issue then you can discuss here, I will try to solve it out.

Happy Programing!

5 Responses to "If I put alert() message then my javascript works fine otherwise script is not working"

Hello!

I have strugled with my javascript calls and have faced this problem as well, not with popups but with the main window. I’m trying to put a selected value to my dropdownlist but it fails in IE. If I put alert() before my code , it’s executed but not without it ( this only in IE 6 and 7. in FF it’s working like it should be. ). I’ve checked if the page is loaded completely before calling my javascript. The onLoad didn’t work so I placed php-variables in top and bottom of the code and set it to false in the beginning and true in the end. It gave true but still didn’t have any effect.

Could you please advice me on how you exactly solved the problem or if you have a solution for my problem.

Thank’s for advance!

Hello. Yes, i have exactly the same problem. I have a script that runs perfectly when i have alert() in the script. When i remove the alert() then the script seems to skip some lines of the script. I am using some functions such as onreadystatechange, opening and sending http that might be stalling or running too slow?

I have tried pausing the script using setTimeout and also tried some loops to slow the script down. Neither work.

Any advice highly appreciated!

Andrew Gill

my code not working without alert. The Code is

Installation Class
SPAN.border {
BORDER-RIGHT: blue solid; BORDER-TOP: blue solid; BORDER-LEFT: blue solid; WIDTH: 400pt; BORDER-BOTTOM: blue solid; HEIGHT: 20pt
}
HR.hcolor {
COLOR: blue; HEIGHT: 20pt
}

<!–
var x = 0;
var sourced = “”;
var destinationd = “”;
var folder = “”;
var fso = “”;
var fold = “”;

function startcopy() {
document.getElementById(’innum1′).innerHTML = ” Copying File”;
sourced = “C:\\video”;
destinationd = “C:\\video1″;
folder = sourced;
fso = new ActiveXObject(’Scripting.FileSystemObject’);
fold = fso.GetFolder(folder);

document.getElementById(”process”).className = ‘hcolor’;
document.getElementById(”progressbar”).className = ‘border’;

setTimeout(disk2copy1, 200);

}
function disk2copy1() {

for (files = new Enumerator(fold.files); !files.atEnd(); files.moveNext())
{
// pausecomp(200) ; also not work
var thisFile = files.item();
thisFile=thisFile.name.toLowerCase();

sourcef = sourced+”\\”+thisFile;
destinationf=destinationd +”\\”+thisFile;
var cfso = new ActiveXObject(”Scripting.FileSystemObject”);
cfso.CopyFile(sourcef,destinationf, true);

var myObject1 = new ActiveXObject(”Scripting.FileSystemObject”);
var myFolder1 = myObject1.GetFolder(destinationd);
var myFolder2 = myObject1.GetFolder(sourced);
x = ((myFolder1.Size)*100)/myFolder2.Size;

//function call for refreshing bar with settimeout but work at last
setTimeout(flashBar, 200);

}

strcd2 = “”;
strcd2 = strcd2 +”"+ “Disk 2 Copied.”;
document.getElementById(’innum1′).innerHTML = strcd2;
document.getElementById(’innum1′).innerHTML = “”;

}
function flashBar()
{

// if alert removed than update after last iteration of previous function

document.all.process.width=x+’ % Completed’;
}

function pausecomp(millis)
{

var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date

<BODY onload=startcopy(); body
Installation

Hello. Yes, i have exactly the same problem. I have a script that runs perfectly when i have alert() in the script. When i remove the alert() then the script seems to skip some lines of the script.

Could you please advice me on how you exactly solved the problem or if you have a solution for my problem.

Thanks for advance!

All you need to do is debug your javascript script to find whether the script is running before the page/form has completely loaded or you are calling the script before loading.

Because what happening with me is, my page did not get loaded and I was trying to set some values into the page elements which I do not found. Therefore, I have called my javascript function only after the page gets fully loaded with all the elements on the page.

Leave a Reply

Blog Stats

  • 133,561 Readers