Popular posts

Pages

Friday, November 29, 2013

Awesome tutorial on Perl Hashes

If you do programming in perl you know how useful Hashes are! Found this cool tutorial and I'm amazed to see so many ways in which Hashes can work for me.... check it out

http://www.perl.com/pub/2006/11/02/all-about-hashes.html

Saturday, September 7, 2013

Regular expression in javascript / jquery

In this small snippet I'm going to show usage regex in javascript (with jquery). I'm not going to explain the usage and importance of regex in programming. There are entire books written solely on regex :). All I can say is regex can be one of the most powerful tools in your coding toolkit.

So here is the situation: I've got a website. A text <input> inside a form and drop-down <select> object. Now based on the change in <select> I want to replace some content in the <input> text value.

HTML
<select>
    <option value="SEV2/min">sev2/min</option>
    <option value="SEV3/maj">sev3/maj</option>
    <option value="SEV4/cri">sev4/cri</option>
</select>
<br>
<input id="sms" type="text" value="bharti, sev: SEV2/min,circle: bihar,impact: voice" />

JS
var re = /sev:\sSEV\d\/[a-zA-Z]+,/;
$("select").on('change',function(){
    var newsev =  $(this).val();
    var text = $("#sms").val();
   
    var newtext = text.replace ( re, "sev: " + newsev + "," );
    $("#sms").val( newtext );
});

Once the <select> is changed, the 'sms' text becomes for ex:

[bharti, sev: SEV4/cri,circle: bihar,impact: voice]

jsfiddle link: here

The html is straight forward. Coming to JS now, here is the sequence:
- define a 're' variable which will be our regular-expression PATTERN
- get the value of <select> drop-down, everytime it is changed
- get the text value of "sms" element.
- populate a variable 'newtext' using the function str.replace( re-pattern, "new string" ). The replace takes two arguments. first, the pattern 're' (or actual string, eg: "sev4"), and second the string to replace matched text. After this call, the 'newtext' contains the updated string.
- set the value of 'sms' element to 'newtext'

This is all about replace(re,"new str") function, hope you enjoyed it!

Thursday, August 15, 2013

Java Web Start error - unable to launch the application

While going through some online java tutorials (at oracle) I found two ways of running them. 1> Java Web Start, 2> Copy the code onto your computer and compile.
Java Web Start is the latest technology where java code at server will run on your laptop. I was going through 'Swing' (GUI/browser) tutorials and I selected option 1.
Now when the 'jnpl' file is downloaded on your machine, it invokes the required JRE and will try to run the program.

The problem I was facing is, when trying to execute the JNPL file, I was getting error 'unable to launch the application' and in 'details tab' I found that it was unable to find JRE1.7, even while I've got the needed jre installed. To find out your installed jre version, on cmd type 'java -version'. This problem was stopping me from running tutorials using Java Web Start.

Below solution worked for me (windows 7)
1> Control Panel > Java icon
2> General tab > Temporary Internet Files > Settings. This will pop-up a window with 'Delete Files' button. Delete the pre-selected two options.
This should work generally, but my problem still persisted. Then proceed to 3
3> In step 2, copy the Location of temporary files. (for me: C:\Users\IBM_ADMIN\AppData\LocalLow\Sun\Java\Deployment\cache) and go to that folder.
4> Delete whatever is there. For me, there was a folder '6.0', maybe because I've another JRE1.6 running on my machine.
5> Once I deleted this folder, and again tried running the JNPL file it worked flawless for me!

Hope this works for you too!

Sunday, July 28, 2013

Autorefresh page (or form submission) using jQuery

In this post you'll see how 'auto-refresh' feature can be put in a web-page (JSP here) or a form submission.

HTML
<span id="spanautorefresh">Refresh in:&nbsp;<label id="clock"></label>&nbsp;</span>

Above HTML is placed in some part of the page for user to know when the page is going to refresh. We'll use the 'clock' label to display remaining seconds to refresh.

jQuery
In our $(document).ready(function(){});, we'll put our jquery function which goes like below

// base functionality start
    var refresh=false;
    var refreshinterval=parseInt(60);
    clock = $("#clock").text(refreshinterval);


    var refreshfn = function autorefresh() {
        if(refresh) {
            var sec = parseInt(clock.text());
            sec = sec - parseInt(1);
            clock.text(sec);
            if(sec<1) {

              clock.text(refreshinterval);
              makeRequest();
              //window.location.reload();
            }
        }
    }; 


setInterval( refreshfn, 1000);

// base functionality ends


//Below 'click' handle is to 'pause' the timer
    $("#spanautorefresh").on('click',function() {
        if(refresh) refresh=false; else refresh=true;
    });


Now lets go through the code. First two lines are used to set the variables for status (refresh=false) and refreshInterval (in our case 60 seconds). Next we're storing the 'clock' label in clock variable for future use. The actual magic is done by the variable 'refreshfn', which is our function. Since we've set the refresh variable as 'false', as the page loads for the first time the timer is off. Once the user clicks on the span (which houses clock) the refresh variable is set to true.
The function 'setInterval( fn_name, milliSeconds ) is standard JS function which invokes the fn_name every milliSeconds ms. In our case it invokes refreshfn every 1000 ms or 1s. When this happens refreshfn is called and if refresh is 'true', the value of clock is reduced by 1. The IF block inside refreshfn checks the text value and if it is less than 1, a> sets the clock text back to 60, and b> calls makeRequest() function in this case. For standard auto-refresh, simply call the window.location.reload() JS function. In this case makeRequest() is doing jquery AJAX/POST request, sending a form data to page X and getting some values to populate a DIV element in current page.

Auto refresh can also be enabled in standard JSP using response headers. But jquery gives you much more control over the functionality and can be triggered by user's action and page state.

I hope you find this functionality useful and easy to implement!

Saturday, March 30, 2013

Taking the road ahead

Quite often, while making a decision, I find myself in deep thought over the available options. This is not uncommon, given the fact that life today throws at us a plethora of choices...
Its not wrong to think, not at all. And that decision, however small, still affects our life. Sometimes for a short while, choosing to go for a boring movie, or an year, buying a pair of super comfy shoes. Or for the whole life, say deciding over a life partner.

The problem here is, some of us (myself included), waste an extraordinary huge amount of time to decide over things. Most of the time, things that are not worth giving a tenth of the time they stole. We take our sweet time and keep meditating on them. What we forget in this process is, total time that we have is limited. Which makes it imperative, that we make most of the available time. Now if we spend more time on deciding whether to take choice A or B, where is the time left to exercise/enjoy the outcome of our decision, or maybe to rectify? For every one second added to the decision making process, one second is deducted from the time we could have utilized our life after that decision.

So, to maximize the available time, we need to allocate the right amount of time to decision-making and move on. We just cannot endlessly keep the decision open. But a question arises, if I take a hurried decision, would that not affect the quality of it? Isn't that risky? That was my way of thinking too. I would rather keep the decision open, than to choose an option which did not have 100% of my heart. Unfortunately it is the wrong approach. One, precious time is wasted. Two, the whole purpose that decision came to me, at that precise time was because it would have brought a value-add to me and my life at that point in time. When I keep the decision pending for a long time, that value diminishes gradually. So whole of that exercise becomes a waste.

The answer is: we have to take a leap of faith. Even if a single option does not emerge as the "winner", we must go with the one which seems to be closest, and trust our instincts. Anyway, even after much deliberation we could not conclude, which means there are (and will be) unknowns involved. So its better to take the leap, free our mental energy and concentrate towards other meaningful stuff. Because while that decision is open, we are unable to live life to the maximum. When we make this decision with this approach, suddenly everything changes. The situation comes under our control. The time we were about to spend further deciding, we can now utilize to drive our actions in favor of the selected option and make most of our life. This single fact, truly means a lot. Its like, once we take this decision, it brings us back into the game, where moving ahead is the mantra, and stopping is game-over.