yesJames.com
ahuh… sure… what ever you say…


Firefox compatible xPath functions in JavaScript

Posted in Ajax,JavaScript,programming by james on November 19th, 2008

There are a lot of quirks between different browsers, in particular though are the differences in the way each browser handles the DOM in JavaScript.

Internet Explorer, for the most part, implements MSXml 4.0 or higher. However that’s (obviously) a Microsoft technology, and the standard implementation doesn’t support XPath in the DOM.

In particular the selectNodes() and selectSingleNode() functions are of particular use when manipulating XML or the DOM. Using these functions you can parse an XPath expression and get a nodelist of elements in your DOM that match the expression.

(more…)

JavaScript Trim() function

Posted in JavaScript,programming by james on March 20th, 2008

Trim(), is one function that we all use almost every day in most languages, whether it’s a core function of the language you’re using or a member (or prototype in JavaScript) function of the String object itself (as in C#), it’s invaluable for developers.

Unfortunately JavaScript doesn’t have this sort of thing built in, so here’s how to implement your own trim() functions (as well as ltrim() and rtrim()) in various ways depending on the level of backward compatibility you require.

(more…)

howto: GET a URL parameter in JavaScript

Posted in JavaScript,programming by james on February 11th, 2008

A useful little piece of JavaScript for retrieving a URL parameter (Query String) from your page. If a requested parameter doesn’t exist it will return an empty string rather than a NULL object. It will also handle the inclusion of anchor tags in the URL and exclude them from possible results.

e.g. http://www.yesjames.com/test.php?myparam=justatest
calling “gerURLParam(‘myparam’) will return “justatest”

function getURLParam(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var pattern = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(pattern);
  var aResult = regex.exec(window.location.href);
  if ( aResult == null ) {
    return "";
  } else {
    return aResult[1];
  }
}