JavaScript: calling a method and changing what “this” means

This post has been moved here

Disabling selection of a section

Found this little gem while trying to prevent selection behind the jQuery.treeview controll.

onselectstart="return false;" style="-moz-user-select: none;"

This worked for me on Chrome, IE 8, and FireFox 3.6.

Found this tidbit here.

Update: 2010-07-05 – Found this interesting extension to jQuery also. Note the replies also as they may be of use.

JavaScript Errors Properties & Throwing Exceptions

Quick bit of information on JavaScript errors and throwing errors. (This post uses error and exception as synonyms… not sure what the correct term is in JavaScript)

I was trying to figure out how to get details from an exception thrown in JavaScript and it seems the structure is like this:

{
  message : “the error message”,
  fileName : “file:///c:/User/me/Desktop/Stuff.js”,
  lineNumber : 57,
  stack : “bunch of stuff here”
}

(Used json2.js to stringify the error message as Firebug didn’t seem to be showing it… ?)

When you throw your own exceptions you can of course throw anything including a string like this:

throw “This is a string”;

… and when you analyse what you caught it will be a simple string with that value.

try {
    throw "This is a string";
} catch(error) {
    alert(error);
}

The result will be an alert box with the value "This is a string" displayed.

Just found out here that you can do this:

throw new Error(reason);

getElementByID in IE 8 compared to previous versions

Found this post about the getElementByID method in Javascript by accident and thought it would be worth posting about.

The basic issue is is that IE 8 compares the id’s is a case-sensitive manner whereas previous versions of IE compare in case-insensitive manner when IE 8 is in actual IE 8 mode. (MS Doc Here)

I always try and keep my identifiers in the same case in all cases, and this is just another reason for me to keep doing that!

Moral of the story: No matter what you do and what language you work in you should endeavour to keep the casing of variables, css classes, uri etc, in exactly the same case in all instance. This helps when in the future something like this happens.

JavaScript Unit Testing Frameworks

A few days ago I found out about FireUnit and then QUnit… John Resig listed a heap of JavaScript unit testing frameworks hear.

He is apparently going to do a detailed blog post of on the results of a poll that he ran to get the list. I hope to add that link here when he is done.

JavaScript eval function with JSON

This post contains a simply example of how to use the eval function in JavaScript to make a string containing JSON into an actual JavaScript object.

The example JSON was from here and below is the code.

Please note:

The parenthesis are required because of an oddity in the JavaScript syntax. I’m unsure of the reasons at the moment. It is briefly mentioned on this page.

It is probably better to use this -> http://www.json.org/json_parse.js … than to use eval though as it is a parser. Eval seems to be considered less secure though it is probably faster.

<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">

var jsonString = '{ "glossary": {"title": "example glossary", "GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"]},"GlossSee": "markup"}}}}}';
var title = eval('(' + jsonString + ')').glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para;
alert(title);

</script>
</body>
</html>

JQuery Visual Studio documentation only on Debug

I’ve included my scripts using a dynamic method that means Visual Studio can’t determine what scripts are available to a given page.

This removed the code completion for scripts, and I found the code completion helpful especially as I am learning JQuery and also due to the fact that you can never remember everything 🙂 I also wanted to prevent the script loading when

To get around this issue I’ve included the scripts like this

<% #if DEBUG %>
<!-- Load VS JQuery documentation if in debug mode! -->
<script src="../../Scripts/jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
<% #endif %>

Which ensures that the Script include is only included in DEBUG builds… this means that when I would go to production that script would not be included in the page…

This doesn’t seem (at the moment) to cause issues with the real registration of JQuery… It’s not the most perfect, but it’s a safeguard  against me forgetting 🙂

Update: It’s actually not being found at runtime… So maybe that is why it is not causing issues 🙂