Archive for the ‘JavaScript’ Category

Firefox Add-On – NoScript

I recently received a new computer at work. One of the first software packages that I installed was FireFox, and made it my default browser.

I then installed an Add-On called NoScript. This turns off all JavaScript on a web page. It gives you the option to either temporarily or permanently turn on the JavaScript for a given site.

For example, I have JavaScript permanently turned on for the ISU Extension web site. I also have it turned on for some of the other sites I visit regularly. On other sites, I will choose to temporarily allow JavaScript.

I never allow JavaScript from certain sites, especially when the JavaScript is trying to run from a third party site. Another piece of JavaScript that I never allow is the one from Google Analytics. Google knows enough about me without me giving them that much information about my browsing habits.

For many pages, loading them without running the JavaScript doesn’t affect them too much. For others, it will change the functionality of the pages, but they still work.  For example, on the eXtension home page, the rotating images simply become a series of individual images. You can still navigate around the site without any problem. For other pages, not having JavaScript enabled completely breaks them.

Here are my reasons for running NoScript:

  • Many pages are less cluttered. A lot of times, the ads and other annoying “features” are implemented in JavaScript. With NoScript, you don’t see a lot of this stuff.
  • Ability to block Google Analytics, and other tracking systems from seeing where you are browsing.
  • Most importantly, security.  Many times a hacked page will try loading a JavaScript file from a third party server. If you don’t allow scripts from unknown sources, they won’t run.

Here is where you can download NoScript.

Connecting JavaScript with a TextBox

Yesterday I was working on a web application where we wanted to have a JavaScript function run when the user changes a TextBox. It was a simple function, which took three input values and performs calculations to generate two additional values. I soon discovered two issues when working with JavaScript in ASP.NET. The first is that the TextBox control is server-side, so it doesn’t include the client-side “onclick” attribute. Second, is that when .NET generates the input box, it adds the ID of the parent container to the ID of the TextBox, for example ID=”TextBox1″ may become ID=”ctl00_ContentPlaceHolder1_TextBox1″.

Doing an Internet search, I found two solutions to the first issue. One option is to include the attribute onchange=”myJavaScriptFunction()” in the TextBox control. Visual Studio will give you a warning, but it does work. The second option is to include the following line of code in the Page_ Load function:

TextBox1.Attributes.Add("onchange", "myJavaScriptFunction()");

I prefer this method because Visual Studio doesn’t flag it.

To solve the second issue, I found an example where they used the ClientID attribute of the TextBox Control to find the element in JavaScript. Here is a sample JavaScript Function that does a popup alert, giving the value that was entered in TextBox1:

function myJavaScriptFunction()
{
  var myField = document.getElementById("<%= TextBox1.ClientID %>");

  alert(myField.value);
}

This is a kind of ugly, but it works. You can create as many variables as you need to point to your .NET controls. The function I wrote yesterday had 5 of these temporary variables. Seems like a lot of work to assign values to a couple readonly fields in the form.