RSS

Category Archives: javascript

common errors and tips while coding in js

Have to Show -> “Please wait while your file(s) upload”

While uploading files have to show message “Please wait while your file(s) upload” with a gif rotator to keep user awaited in case of large file upload. Not a heavy functional file status uploader is required only file uploading in progress is to be shown. If this is what required then this post is definitely for you.

Well you have an upload button like

< input type="submit" value="Upload" onclick="showRotator();" />

Yes, only add onclick event to show a div that contain a rotator with message to prompt user about the uploading.

< div id=”rotator” style=”display: none;” >< img alt=”” src=”loading.gif” width=”20px” height=”20px” />  Please wait while your file(s) upload.< /div>

And here goes the js function

function showRotator()
{
document.getElementById('rotator').style.display = 'block';
}

Pretty Simple 🙂

 
1 Comment

Posted by on April 30, 2013 in javascript

 

[jQuery][error][resolved] GB_CURRENT is null | popup when called from other than onclick didn’t close the popup.

While using Popup jquery plugin, i need to show the popup on text selection instead of that conventional onclick of a link as

getSelected = function() {
var t = '';
if (window.getSelection) {
t = window.getSelection();
} else if (document.getSelection) {
t = document.getSelection();
} else if (document.selection) {
t = document.selection.createRange().text;
}
return t;
}
mouseup = function() {
var st = getSelected();
if (st != '')
{
GB_showCenter('Show popup',url,480,720);
}

}

The issue was when the popup is there in result of onclick of a link, it functions well but when it get called on text selection then its fine except the close function, it gives “GB_CURRENT is null” error and didn’t close the popup window.
I tried multiple ways and the most appropriate was to hide the link and to trigger onclick of the link automatically like

jQuery("div-a").prepend("<div style='display:none'>
<a id='popup' href='{url}' 
onclick=\"return GB_showCenter('Popup', this.href,480,720)\"
;>show popup</a></div>");
document.getElementById('popup').onclick();

Even this didn’t work.
Finally i hide the close link of greybox and added custom close link instead as

<img src="{basepath}/greybox/w_close.gif">
<a style="text-decoration:none;color:grey" 
href={url} onclick="top.window.location=this.href;">Close
</a>

and it closes the popup 🙂

 
Leave a comment

Posted by on November 23, 2011 in javascript

 

Tags: , , , , ,

|| javascript || tutorial || bind events with keyboard shortcuts

For relating keyboard short cuts with checking or unchecking the checkbox, i once need to get the value of key pressed. Following is the simple code for getting the ascii of any key pressed.

<script type="text/javascript">
$(document).ready(function() {
jQuery(document).keydown(function(e) {
alert(e.which);
});
});
</script>

For binding event with multiple keys like Ctrl+a, following code is there to help you out,

<script type="text/javascript">
$(document).ready(function() {
var ctrl = false;
jQuery(document).keydown(function(e) {
if(e.which == 17) //17 = ctrl
{
ctrl = true;
}
if(e.which == 65 && ctrl) //65 = a
{
alert("You have pressed Ctrl + a");
}
});
});
</script>

Thats it 🙂

 
Leave a comment

Posted by on October 19, 2011 in javascript, php

 

Tags: , , , , ,

[ javascript in php ][ tip ] nested quotes

Once working with js code within php echo, i got totally lost when i have (double quotes(single quotes (double quotes(single quotes)))). Its like an onion and remind me a question in my fsc-II maths books having infinite square roots. I was thinking of inventing something new other than ” and “”. so that to quote string at third level. Code like following

echo " < a href= ' # ' onclick=' alert( " 123 " ) ' >LINK</a>";

was there with syntax errors and errors like
-Error: missing ) after argument list
-unterminated string literal
Resolution:
It is then resolved by a simple back slash(\) followed by a ‘ or ” as per case.
now its like

echo " < a href= ' # ' onclick=' alert( \" 123 \" ) ' >LINK</a>";

Also something about variable within single and double quotes:

$str = 'simple text'
echo '$str'; // $str
echo "$str"; //simple text

Means variable within single quotes is treated as a string and variable within double quotes is treated as a variable.

 
Leave a comment

Posted by on October 19, 2011 in javascript

 

Tags: , ,

|| javascript || tip || javascript code is in-affective

If you find that some of javascript (js) code is not executing and the positioning of the code is also playing its part i.e. you put the code at the top it works and at the required position its not working, then most probably there is an error in the js file. As in php any syntax error leads to dead blank page similarly any error in js code makes the remaining js code  in-affective. For tracing the error log, you can follow any of two options:

1) Check the error details at Tools->Error Console

2) Click on firebug and check the Console menu.

So, along with the code under observation you have to check other js code in the file if you find your js code quite 🙂

 
Leave a comment

Posted by on October 18, 2011 in javascript

 

Tags: , ,

|| jQuery || error || solution || $ is not defined

Errors like “$ is not defined” or “jQuery is not defined” are removed by including the following google api, instead of ist copying and then calling its better to simply call 🙂

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6
/jquery.min.js"></script>
                           OR
<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"
 type="text/javascript"></script>
 
Leave a comment

Posted by on October 18, 2011 in javascript

 

Tags: , , , , ,

 
%d bloggers like this: