Jim Rogers

Lives in Baton Rouge, LA, with two dogs, one cat, and one lovely wife. I'm a lead developer for GCR Incorporated.

Katrin and Jim

Month List

Context switching with jQuery

by Jim Jan 29, 2011 8:38 PM

My current project requires me to make a lot of asynchronous calls with javascript, and even to chain these calls. After a lot of refactoring I’m comfortable with the way I’m handling this, but I spent a while fighting with context and having the ‘this’ keyword point to something different every time I used it.

I’m using jQuery for this project. Part of my solution is to explicitly switch context at certain times; the means of doing this is not immediately obvious, I think, from the jQuery documentation, but it’s simple enough.

Given a context object with a function pointer, some data to pass as an argument, and an object whose context I want to switch to (i.e., that object will be the ‘this’ in the invoked function,) I do this:

var p = $.proxy(context.method, context.reader);
p(context.data);

Parse a little-endian

by jim Jan 01, 2011 3:44 PM
littleindian

What? No, e-n-d-i-a-n. In the computer world, it’s not unusual to find integers represented in reverse byte order, with the least significant digit first. For example, the hex representation FF01 might be two very different numbers, depending on whether it is big-endian (the way we’re used to seeing them in base 10):

base 16:            FF    01
base 10:           255   001
times position:    2^8   2^0
add to get int:  65280 +   1  =  65281

or little-endian:

base 16:            FF    01
base 10:           255   001
times position:    2^0   2^8
add to get int:    255 + 256  =  511

The javascript parseInt() method assumes a big-endian representation in the string passed to it. Here’s a method that parses a little-endian hex string.

function parseLittleEndian(hex) {
    var result = 0;
    var pow = 0;
    while (hex.length > 0) {
        result += parseInt(hex.substring(0, 2), 16) * Math.pow(2, pow);
        hex = hex.substring(2, hex.length);
        pow += 8;
    }
    return result;
};

Anonymous Access with WebResource.axd

by Jim Sep 14, 2007 5:34 PM

We have a site that uses forms authentication, with the exception of a few pages. In one of these pages, we were getting a bunch of syntax errors and weird behavior, but only when the page was accessed anonymously. 

Firebug was showing syntax errors in the ScriptResourse.axd file. This is supposed to programmatically render script for the UpdatePanels and such, but was rendering the login.aspx page instead; authentication had failed for the anonymous user, and the request was redirected to the login page. Of course, this is html and not valid javascript, thus the syntax error.

Because the script includes didn't render, and didn't define the classes needed for the UpdatePanels, the page also had errors on the following lines:

Sys.Application.initialize();
Sys.WebForms.PageRequestManager._initialize('ScriptManager', 
document.getElementById('MasterPageForm'));
var mngr = Sys.WebForms.PageRequestManager.getInstance();

This solution to this issue is to allow anonymous access to the ScriptResource.axd "file" in web.config, like so:

<location path="ScriptResource.axd">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>

And while you're at it there's a WebResource.axd as well:

<location path="WebResource.axd">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>

LinkButton UpdatePanel & onbeforeunload

by Jim Aug 29, 2007 10:13 PM

I'm back to ASP.NET programming having fun with a highly complex user interface.

Our site uses UpdatePanels to bring in new content and we're warning on navigation away from the page - including the back button - by returning a message in onbeforeunload. But all of our ASP.NET LinkButton controls were triggering this warning, even when posting back to the same page. Here's my test page:

<%@ Page Language="vb" Strict="True" %>
<html>
<script>
  var showMessage = true;
  function beforeunload()
  {
    if (showMessage)
      return "You are leaving the test page."
  }
</script>
<body onbeforeunload="return beforeunload();">
  <form runat="server">
    <a href="javascript:function() {showMessage=false;}">Anchor</a>
    <hr />
    <a href="#" onclick="showMessage=false;return false;">Anchor</a>
    <hr />
    <asp:LinkButton runat="server">Default LinkButton</asp:LinkButton>
    <hr />
    <asp:LinkButton runat="server" OnClientClick="showMessage=false;"> 
      Fixed LinkButton</asp:LinkButton>
  </form>
</body>
</html>

This shows the trick to using a LinkButton with an UpdatePanel, or in any situation where the page posts back to itself. The script for turning off the message must be in the onclick, and not in the href's javascript:. The latter works in FireFox but not in IE.

This is because in Internet Explorer, the sequence of events is onclick, onbeforeunload, href, while in Firefox the sequence is onclick, href, onbeforeunload.

Javascript Query String Parse

by Jim Jan 30, 2007 2:38 PM

This shouldn't be so hard to google. To parse an html page's query string with Javascript, using a regular expression:

// Get the value of one of the arguments in the query string
function getFieldValue(name)
{
    var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(location.search);
    return match ? unescape(match[1]) : "";
}

Returns an empty string if the name doesn't match.