Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Monday, April 21, 2008

Navigating to other pages using JavaScript

There are many different ways of transferring user from one page to another page without opening up a new page which don't necessarily work either IE or Firefox. The following way works with both IE and Firefox.

Simply assign the new URI to href property of location.

document.location.href="http://www.mycomputerknowhow.blogspot.com";

Monday, February 18, 2008

Check the browser to see whether or not cookies are allowed

To see if you can save your cookies on the client's machine, you can simply use JavaScript. I did some search on the internet and realized that some people recommend using code-behind. While there is no need when you can use JavaScript, doing so will require for the web application to post back twice.

Use the following code to specify cookies are allowed. It tries to leave a cookie on the client's machine; if it succeeds, it means cookies are enabled.

document.cookies="MyTest=Test";
if (document.cookies=="")
alert("Cookies are blocked");
else
alert("Cookies are allowed");

Sunday, December 23, 2007

Multiple onload event functions

Sometimes it is necessary to use third party JavaScript libraries in your projects. These libraries might use some of the the JavaScript events. As a result, when you pass a new function to event to have it run when, for example page is opened, the event of library gets overwritten. Therefore, the library won't function correctly. This is the case for some of the online mapping software that use client-side scripting such as MapQuest. Include the following function to your Onload event handler:

var LibraryFunction= window.onload;
if (typeof window.onload != 'function')
//Is there an event handler already attached to Onload event?
{
   window.onload = MyFunction();
}
else
{
   window.onload = function()
   {
     LibraryFunction();
     MyFunction();
   }
}

Tuesday, December 11, 2007

Using iframe in web pages causes the browser back button not to function correctly

Introduction

Being able to embed a web page in another web page can be really handy. Also source page of iframe can be modified which means that programmers can update the embedded page content.

Issue

The problem with iframe tag is that when users click on the browsers back button, the embedded page goes one page back instead of the main page if the source URL of the iframe has been dynamically changed. This behavior is not acceptable in some situations.

Resolution

Use the follow JavaScript code in order to change the iframe source URL:

top.frames['Iframe Name'].location.replace("New URL");

Iframe Name is the name of iframe tag. Please note that it is not the iframe Id.
New URL is a placeholder for the URL name.

It works in both IE and FireFox.

Wednesday, November 7, 2007

HTML area tage and JavaScript onload event problem

Issue

Using JavaScript onload event of HTML area tag, can be problematic.

Workaround

Instead of having onload event, utilize href attribute tag to address this problem. Simply change the code to:
<area href="javascript:MyJavaScriptCode" ...

JavaScript Onload event on Visual Studio 2005 Content pages.

Issue

If you create Master/Content page setup in Visual Studio 2005, you quickly realize that Content pages do not include essential HTML tags such as Head and body. Therefore, Onload event of JavaScript cannot be used on the body tag.

Workaround

Use the following in you Content page:

<script type="text/javascript" >
  window.onload=MyFunction;
< /script>

MyFunction is a placeholder for your function name.

Note: there are no parentheses preceding the function name. If parentheses are used in front of the function name, user will recieve JavaScript runtime error "Not implemented".

Thursday, November 1, 2007

Removing special characters from query string values using javascript

Query string parameters can not include specific characters. One of the easiest ways of removing these characters, is through using JavaScript encodeURI function.

var encodedString=encodeURI("URI String");

To get the original string use the following function:

var decodedString=decodeURI("URI%20String"));

Saturday, October 6, 2007

Cancelling Javascript events

event.returnValue= false;