Posts Tagged ‘javascript’

How to block keyboard keys in forms with javascript

A client asked me to block the ENTER key in a form I created for him. After some research, here is the piece of code you have to enter into the page, which will block the event:

<script type=”text/javascript”>
$(document).ready(function() {
document.onkeypress = stopRKey;
});
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type==”text”))  {return false;}
}
</script>
The 13 in the code above is the code for the ENTER key, replace that if you want to block an other keypress.

Get Client IP with Javascript

Ever wondered how to get the client’s IP address from Javascript? I have to set it in a Cookie variable now, so here is what I found, a clean and easy solution:

<script type=”application/javascript”>

function get_my_ip(json){
alert(json.ip);
}
</script>
<script type=”application/javascript” src=”http://jsonip.appspot.com/?callback=get_my_ip”></script>

Fancybox auto popup on document ready with jquery

Last week I had a really interesting task, to display a popup window when the page is loaded. As the Fancybox jQuery plugin was already used in the project, I decided to use it for this task also. It was an adventure, as I’m not so familiar with the Fancybox, but here is the solution:

I created a hidden anchor in the html, something like:

<a href=”the_url_from_where_the_popup_window_is_loaded” title=”" onclick=”" id=”youtube_popin” style=”display:none;”></a>

In a javascript file which is previously declared, I added:

$(document).ready(function(){
var t=setTimeout(“show_youtube_popin()”,1000);
});

function show_youtube_popin(){

$(“#youtube_popin”).fancybox({

‘width’ : 760,

‘height’ : 330,

‘autoScale’ : false,

‘transitionIn’ : ‘none’,

‘transitionOut’ : ‘none’,

‘type’ : ‘iframe’,

‘hideOnContentClick’: true,

‘hideOnOverlayClick’: true

});

$(‘#youtube_popin’).trigger(‘click’);

}

Notice that the Fancybox is not called as the page is loaded, but after 1 second. If I did not use this timeout, I got a javascript error from the plugin (I think it wasn’t really loaded yet…)

After the Fancybox is applied to the hidden anchor element, i will trigger the click action on it with jQuery… And the popup window appears… SUCCESS! :)

Javascript replace all string

Have you ever wonder if there is a similar command to str_replace from PHP in Javascript? There is a replace command, which works like:

str = str.replace(”replace_what”,”replace_with”);

The problem with the code above is that it replaces only the first occurrence of “replace_what”, so if there are 2 or more occurrences of it, won’t replace them. To fix this, use the following:

str = str.replace(/find/g,”replace_with”);

Check all uncheck all HTML checkboxes with Javascript

The following little piece of code shows how to check all / uncheck all HTML checkboxes with simple Javascript, without jQuery. It’s not something hard to do, but as I use it a lot, it’s nice to have it at one click…

<form name=”formname” method=”POST” action=”?action=dosomething”>

Check all: <input type=”checkbox” name=”checkall” id=”checkall” value=”Check All” onclick=”Check(‘formname’,$(‘.check_list’))”>

Option 1<input type=’checkbox’ class=”check_list” name=”check_list[]” value=”1″>
Option 2<input type=’checkbox’ class=”check_list” name=”check_list[]” value=”2″>
Option 3<input type=’checkbox’ class=”check_list” name=”check_list[]” value=”3″>

</form>

The corresponding Javascript code:

function Check(form_name,chk){ // alternate check all/check none of items

if(eval(“document.”+form_name+”.checkall.value”)==”Check All”){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
eval(“document.”+form_name+”.checkall.value=’UnCheck All’”);
}

else{
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
eval(“document.”+form_name+”.checkall.value=’Check All’”);
}

}