Category Archives: JavaScript

Debugging JavaScript in asp.net User Controls

Little while back I was facing issue is debugging JavaScript code written inside an asp.net User Control (ascx) file in visual studio 2010.

Interesting thing is that breakpoints doesn’t work for User Controls in asp.net but you could still do the debugging if you write debugger; keyword before the line where you want to set the breakpoint. The visual studio will automatically detect this keyword in your code and would pause the execution at that line in debug mode.

But in my case even the debugger keyword was not working. Whenever visual studio designer used to encounter this keyword, VS used to halt at the first line of page’s markup (pagename.asps[dynamic]), and by this I could not find that exactly which of my debugger statement got hit.

The issue seems to be of the page’s temporary file/markup generated by VS.

Solution

deleted the website’s .pdb file from project’s bin folder and things started working.

Hope this information would help someone other there in coding world.

Thanks


Run JavaScript after page is loaded

If you need to call a JavaScript code when the html page is completely loaded, you can use JQuery.

At times some controls are not rendered and the JavaScript code runs, hence it is  not able to find controls which is not rendered yet and client side code throws exception.

Use $(document).ready feature of JQuery as belows:

<script language=”javascript” type=”text/javascript”>

$(document).ready(
function()
{
showMsg();
}
);

function showMsg()
{
// your code here
}

</script>

The above is also a replacement of the old body onload property: <body onload=”javascript showMsg()”> thing


this._form is null or not an object, JavaScript Error

I was getting below javascript error everytime i run my visual studio asp.net web pages:

this._form is null or not an object

Because of this error all my AJAX controls and page postback stopped working.

As the error clearly states that it is a JavaScript error so i started checking all my Javascript code.

Finally searching on the internet i found the cause and cure of this problem.

Cause:

If you are including javascript files in the page header like an empty tag , like below :

<script src=”aj.js” type=”text/javascript” />

Solution:

Here the closing of script tag is not correct, through its an empty tag but somehow it needs to be closed as a old fashioned way, like done below:

<script src=”aj.js” type=”text/javascript”></script>

Close the javascript tag properly and see the magic yourself.


Preload images in HTML using JavaScript

Preloading means loading an object (graphics, text, movie) before showing it to output.

A good example is of  preloading in flash files, when you see a loader before displaying the content, and after loading site shows a smooth experience of flow of data.

This is mostly used when there is an mouser over images to be loaded. If user mouse overs an image and then you loads the new mouse-over image it may take a few seconds to load, which leads to a small blank image over the image area. By pre-loading the image you can show mouseover image instantly without any delay.

In this post I am explaining how to do preaload imags in HTML using JavaScript, without using flash.

We have to use the Head tag of html as this is the one which loads on first page executiution.

<head>
<script type=”text/JavaScript”>

if (document.images)
{
pic1= new Image(190,53);
pic1.src=”images/productSmall.gif”;

pic2= new Image(190,53);
pic2.src=”images/storeSmall.gif”;
}

</script>
</head>

The above code lods two images naming productSmall.gif and storeSmall.gif respictively from images directory. It creates a new object of image and assigns it a dimention and a file to load.

The document.imags condition has been used to determine that the browser supports the images or not.