JavaScript Tips n Tricks: javascript:void(0)

Two options

<a href="#" onclick="myJsFunc(); return false;">Run JavaScript Code</a>

or

<a href="javascript:void(0)" onclick="myJsFunc();">Run JavaScript Code</a>

Both options do almost the same thing.  By using the second one you will not need to remember to use the “return false;” that is in the first one.  When writing large amounts of complex code using this pattern saves on bugs and time.  One issue with it is accessibility, I will need to do more research on this when the time comes.

StackOverflow question : Href attribute for JavaScript links: “#” or “javascript:void(0)”?

Filter DDL items using javascript and a Dictionary<string, string> object in the ViewBag

I had a need to modify the options on a DDL based on different selections the user had made on the page.  I did this at runtime by passing in a list of menu items in the ViewBag and using javascript to add and remove items based on the users selections.

Step 1: Create a ViewBag Dictionary <string, string> object that contains the full list of possible menu items.

ViewBag.MenuItems = new Dictionary<string, string> {
     {"option1", "Option One"},
     {"option2", "Option Two"}, 
     {"option3", "Option Three"}, 
     {"optionShowMe", "Option Show Only Me"}, 
};

Step 2: Create a javascript function that will insert an item into the DDL

function AddItem(Text,Value)
{
    // Create an Option object
    var opt = document.createElement("option");
    
    // Add an Option object to Drop Down/List Box
    document.getElementById("DropDownList").options.add(opt);

    // Assign text and value to Option object
    opt.text = Text;
    opt.value = Value;
}

Step 3: Create a method that will filter the options based on the users selection

function FilterDropDownListItems() {
    var array = @(Html.Raw(Json.Encode(ViewBag.MenuItems)));
    var userSelectedOption = $('#UserDDL').val();
    $("#DropDownList").empty();

    if (userSelectedOption === '140') {
        for (var val in array) {
            if(val === 'optionShowMe')
                AddRequestTypeItem(array[val], val);
        }
        $('#DropDownList').val('optionShowMe');
    } else {
        AddRequestTypeItem('Please Select', '');
        for (var val in array) {
            if(val != 'optionShowMe')
                AddRequestTypeItem(array[val], val);
        }
        $('#DropDownList').val('');
    }
}

Javascript Plugin Detector code

I spent the day searching for a good solution to detecting plugins such as Shockwave, Flash, Quicktime, etc. The final solution was using http://www.pinlady.net/PluginDetect/. It was as easy as generating the javascript file, adding it to my solution, and adding the following script to my page. <![CDATA[ PluginDetect.getVersion(‘.’); var v = PluginDetect.getVersion(‘Flash’); var isInstalled = (PluginDetect.isMinVersion(‘Flash’, ‘0’) >= 0 ? true : false); if (isInstalled) document.write(“Adobe Flash (” + v + “) is installed.”) else document.write(“Adobe Flash is disabled. Get Adobe Flash.”) ]]>

Custom quote widget for Blogger

On a blog that I have I wanted to have a list of custom quotes that I could have displayed at the top of my blog.  After a bit of searching I came upon this link to a site with a simple widget that did the job.  Here is the code as well.  The only change that I made to it was to add the  to change the color to better match my theme.

http://wishingdoesntmakeitso.blogspot.com/2008/09/quote-widget-for-blogger.html

    
var quotecounter = 0;
var m
/**********************************************************
This code is meant to be installed as an "html/javascript" gadget on a blogger blog.
**********************************************************/
/***************************************************
This code is coprighted by Gove Allen, Ph.D.
Non-comercial use of this code is permiotted provided
that any additions or modifications made are also
made freely available for non-comercial use. For
all use, this comment must remain intact.
For commercial use, contact Gove Allen at
http://gove.net
****************************************************/
var quotes = new Array(); var author = new Array(); var second = new Array()
/*-------------- User Configuration --------------*/
/* the next line allows you to control whether the
link shows to allow the user to pop up a windows
showing all the quotes. Just put the text you want
to show for link in the quotes*/
var labelForAllQuotes = 'View my quotes';

/*-------------- add quotes here --------------*/
/* The first parameter is the number of seconds
the quote should display/ The second is the name
of the person who said the quote. The third is the
quote itself. The second and third are delimited by
either a single quote(') or a double quote ("), you
choose. However, you cannot put the same kind of
delimiter insite the argument that is used to delimit
the argument.*/

q(100, 'Albert Einstein', 'If at first the idea is not absurd, then there is no hope for it.')
q(100, 'Lord Chesterton', 'There are no uninteresting things; there are only uninterested people.')

//-------------- No user-configurable parameters below this line --------------
m = quotecounter;
quotecounter = 0;
function q(secs, auth, quote) {
quotes[quotecounter] = quote
author[quotecounter] = auth
second[quotecounter] = secs
quotecounter = quotecounter + 1
}
function putquote() {
document.getElementById('quotetext').innerHTML = quotes[quotecounter];
document.getElementById('author').innerHTML = author[quotecounter];
document.getElementById('allQuotes').innerHTML = '' + labelForAllQuotes + '';
setTimeout("putquote()", second[quotecounter] * 1000)
quotecounter = quotecounter + 1
if (quotecounter >= m) { quotecounter = 0 }
}
function showQuotes() {
var w = window.open('', 'Quotes', 'toolbar=0,location=0,menubar=0,width=600,height=600,scrollbars=1')
w.document.write('');
w.document.write('Quotes');
w.document.write('');
w.document.write('
');
var o = '"
for (x = 0; x < m; x++) {
w.document.write(o);
w.document.write(quotes[x]);
w.document.write(c);
w.document.write(o2);
w.document.write(author[x]);
w.document.write(c2);
}
//w.document.write(>
"
var c2 = "
';
var o2 = '
';
var c = "
');
w.document.write('');
w.document.close();
}



















putquote()