Search

Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Jun 30, 2009

Get query string value using JavaScript

Here is the JavaScript function getQuerystring which finds the key form query string and returns the value.

/*
* <summary>
* Get the querystring value
* </summary>
* <param name="key">A string contains the querystring key</param>
* <param name="defaultVal">Object which get returns when there is not key</param>
**/
function getQuerystring(key, defaultVal) {
if (defaultVal == null) {
defaultVal = "";
}
key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
var qs = regex.exec(window.location.href);
if (qs == null) {
return defaultVal;
}
else {
return qs[1];
}
}

Apr 2, 2009

Enum in JavaScript


We are using enum in server side scripting language, recently I come across requirement where I have to write lots of if-else if clause, then I found the interesting thing which is enum.

Lets see how we can declare enum.

var Technology = 
{
Microsoft: 0,
PHP: 1,
ROR: 2,
Java: 3
}

Minor change in declaration, now lets see how we can use enum.

function Show(tech) {

var msg = 'Welcome to the world of {0}';
switch (Number(tech)) {
case Technology.Microsoft:
alert(String.format(msg, 'Microsoft'));
break;
case Technology.PHP:
alert( String.format(msg, 'PHP') );
break;
case Technology.ROR:
alert(String.format(msg, 'ROR') );
break;
}

}


//And here is the function call
Show(0);
Show(Technology.PHP);
Show(Technology.Microsoft);
Show(2);



Nov 20, 2008

Getting value form pervious page into new window

I saw lots of question posted on http://forums.asp.net/ about how to get the value from pervious page into newly opened window. Its very simple, you have few ways to pass variables to newly opened window. First and foremost is passing variable into query string.

You can get all full form collection; not only this you can also read the value of global variable of parent page in to newly opened window. Let's see how.

I created one variable into parent page. And somewhere I call window.open() to open new page into new window.

<script language="javascript" type="text/javascript">
var gblPageTitle;
gblPageTitle = 'Welcome to ';
</script>

In parent page I just have to write following line to get that variable.

<script type="text/javascript">
document.title = 'Questions on '+ window.opener.gblPageTitle;
</script>

And now lets get the full document of parent page or specifically form collection.

<script type="text/javascript">
var previousForm = window.opener.document.forms;
</script>

Have fun with window.opener :)

Sep 24, 2008

with operator in JavaScript

Hello Friends,

I found one good thing in JavaScript, there is with operator in JavaScript, which you can say same as with operator in VB.NET. Its always helpful to define short name of big variable or replacement of such big variable; which lead us to make code more readable and maintain too.

In general when we are dealing with XML datasrouce in JavaScript; there always have long long variable name, of the value is so deep inside.

xmlDoc.childNodes[1].childNodes[0].childNodes[3].childNodes[0].text
xmlDoc.childNodes[1].childNodes[0].childNodes[4].childNodes[0].text

We have to write more lines of code to get all the details. Here we can use with, as we know part of path to reach final value is common which is xmlDoc.childNodes[1].childNodes[0], so we can put it into with keyword.

with(xmlDoc.childNodes[i].childNodes[0])
{
name = childNodes[1].childNodes[0].text;
add1 = childNodes[2].childNodes[0].text;
add2 = childNodes[3].childNodes[0].text;
email = childNodes[4].childNodes[0].text;
.
.
.
}

Jul 24, 2008

User Control and Javascript in UpdatePanel

Hi All,

There is general practices where we require some javascript function while useing UpdatePanel; along with the User Control; and the more problamatic is when the User Contorl is dynamic.

Problem:
When we use UserControl which change the state of Ajax postback; sometimes we require some javascript which needs to be embaded after loading the UserControl and with this said I am using UpdatePanel. Means my code is in the UpdatePanel.

Solution:
Generally we are using following code to run any javascript in code behind

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "alert('hi');", true);

Now if you are using UpdatePanel then replace you above code with following code.

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key", "alert('hi');", true);

Now you can see the alert saying 'hi' to you!!

Nov 28, 2007

How to know page validity from java script

Hello All,

I was having one requirement which is as follows.

- Once you click on submit button, the button must be disabled
- All the validator should work properly then and only then button get disabled [obvious thing]
- And the page needs to be submitted back to server as well.

I was looking for the method where I can find the page valid property or value which allow me to do what I want.

There is a property Page_IsValid in java script which let me know the validity of page. But it will always set to false first time.

So I found the solution which help me to fulfill my requirement.

Have a look at following code

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

function btnSaveClientClick(objBtn)
{
var isPageValid = Page_ClientValidate();
if(isPageValid)
{
objBtn.disabled = true;
__doPostBack(objBtn.name,'');
}
}

</script>

This is the server side control [submit button]

<asp:Button ID="btnSave" runat="server" SkinID="button_plain" OnClientClick="javascript:return btnSaveClientClick(this);" Text="Save" OnClick="btnSave_Click" />

How it works:

- On client click of save button btnSaveClientClick() method get executed with single parameter this which is button itself.
- Page_ClientValidate() method used for checking client site validation, will return true or false.
- objBtn.disabled, will disable the button because objBtn is reference to our Save Button
- And last need to postback to server, so call __doPostBack(objBtn.name,'');

That's it!

Feb 14, 2007

JavaScript Verifier (Verify your Javascript)

Website for Verifying your javascript

http://jslint.com/

It reports lots of small mistake which could be avoided and make javascript perfect...

This will help you to use your application in multiple browser