Showing posts with label Java Script. Show all posts
Showing posts with label Java Script. Show all posts

Monday, March 23, 2009

showModalDialog and postbacks in ASP.NET

I learned something new today that window.showModalDialog to open a modal window on the client from parent page using javascript, but whenever that page does a postback, it would open up a new window. A normal window.open works fine, this is the problem only with modal window.

Finally I found a solution which is very simple by just including the below tag in the section of the modal Window.

<base target="”_self”/">

And also we need to set the output cache property to “1″ otherwise next time when you try to open the modal window, it will take it from cache.

<%@ OutputCache Duration=”1″ VaryByParam=”none”%>

Enjoy..

Saturday, October 11, 2008

Java script function to get browser name..

We have this requirement in most of the application to know that which browser user is using to view the web site. We can use javascript to know browser name and this function works with all the broswers...

function GetBrowser()
{
           if (/Firefox[\/\s](\d+\.\d+)/.
test(navigator.userAgent))
           { 
                 var ffversion=new Number(RegExp.$1) 
                 if (ffversion>=3)
                  alert("FF 3.x or above")
                 else if (ffversion>=2)
                  alert("FF 2.x")
                 else if (ffversion>=1)
                  alert("FF 1.x")
            }
            else if(/Chrome[\/\s](\d+\.\d+)/.
test(navigator.userAgent))
            {
                 alert("Chrome.");
            }

           else if(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent))
            {
                 alert("Opera");
            }
            else
            {
             alert(navigator.appName);
            }
             
            return false;
}

 window.onload  = GetBrowser;


Enjoy..............

Thursday, October 18, 2007

To refresh Parent Window from Pop Up Window


Sometimes it is required to refresh your Parent Window through Child Window, Here is a small piece of java script code which will solve your problem.

window.opener.location.reload();

Put this code in any function of Child Window. When that function is called parent window will be reloaded.

Thursday, October 11, 2007

Change Url of Popup without closing it

On one of my project, I encountered a problem.I have 2 buttons on the page and on click of any button i have to open a pop up window.
Let's say on click of button 1 -> pop up window should open with Page1.aspx
and on click of button 2 -> pop up window should open with Page2.aspx.

But the problem was I have to use only one instance of pop up window.One way is I can close the existing pop up window and open another will other Url.But this is not I want.

I want to use the same window and if it is open just change the Url.

After some googling I Found a solution:-

var childRef = null;

function openpopup(page)
{
if (childRef == null childRef.closed)
{
childRef = window.open(page1 , '');
}
else
{
childRef.location.href=page;
childRef.focus();
}
return false;
}

This Java Script function was called onClientClick event of the button and respective page value was passed as parameter.

ChildWindowObject.Location.href = Some Url does the trick for me.It will change the Url of popup.

One thing to notice here..

If I dont use childRef.focus(); this code then what is going to happen If the pop up window is open (not Minimized) and when childRef.location.href=page; this gets excuted pop up window will be minimized automatically.

To keep it not minimized I used childRef.focus();. This will set the focus again on the window.

Enjoy..

Happy Programming...

Tuesday, August 28, 2007

Java Script For IE and FireFox

Currently many browsers are available in the market that is making life hell for programmers.

If syntax A is supported by Browser A then this syntax is not supported by Browser B.

The one very interesting I encountered when I was working on one of my project that is how java script supported by browsers.

There is a slight difference how the HTML tags are rendered by IE and FireFox.

Look at this scenario


<parent>
<Child>Contnet of Child</Child>
</parent>

This is interpreted in different ways by FireFox and IE.

How IE interprets

IE considers <parent>has a single child node called <child>

How FireFox interprets

On every new line, FireFox inserts empty text node. In the above case FireFox considers there are 3 child nodes.

This is how these tags will be rendered in FireFox

<parent>
<Empty Text node 1>
<Child>Contnet of Child</Child>
<Empty Text node 2>
</parent>

So, you see, first Child is the for IE, but an Empty text Node for FireFox.

The solutions may be:-

1. Put Everything in a single line:- <parent> <Child>Contnet of Child</Child> </parent>

But it is very impractical solution.

2. Change the ways you access the nodes.

First of all determine the browser used by the user.


function checkBrowser()
{
var browser=navigator.appName;
if (browser == "Microsoft Internet Explorer" )
{
return 0;
}
else if (browser == "Netscape")
{
return 1;
}
}

Once the browser is determined then return respectively value of the index variable from the function.

Now this index variable will be used to access the control in Java Script.

function checkValue()
{
var index;
var rowNo = 1;
index = checkBrowser();
var o = document.getElementById('<%=dgSystemList.ClientID%>');
var chk1= o.rows[rowNo].cells[0].childNodes[index];
}

Note:-This function is just for sample.

One thing to notice there are two ways to access child nodes.

1. children
2. childNodes

The Problem with the first method is that it is not compatible with firefox. It is working fine for IE.

Second method works fine for both the browsers.

To make your code compatible with both the browsers use childNodes method.
Hope this helps…



back to top