Archive for the ‘Javascript’ Category

Javascript quick tips to do common tasks

31
Jan

When developing for the iPhone and the iPod Touch, the first thing we have to do is obviously detect it, so we can apply specific code or styles to it. The following code snippets will detect iPhones and iPods using Javascript, and redirect those users to an iPhone specific page.

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
    if (document.cookie.indexOf("iphone_redirect=false") == -1) {
        window.location = "http://.................";
    }
}

23
Dec

By default, .js files are considered as files for compiling by xcode. So when we try to access the .js file in the resource, it will be returning a null value.

 

Here is the solution to avoid this issue. Go to the app target, then remove the .js file from the compile sources section and then add the files to the Copy Bundle Resources section. Now perform the Clean All Targets action and remove the app from the device or simulator if it is already installed.

 

Now you are ready to go. Just run the app and now you can see that you can access the .js file from the resource directly.

, ,

07
Oct

The following line of code is used to get the index of activeitem in Sencha

var newIndex = panel.items.indexOf(panel.getActiveItem());

Where,

newIndex -> a variable to store the index.

panel -> object of the panel

If there are 2 items in the panel and the value of newIndex is 0, then it represent the first items and if the value of newIndex is 1, then it represents the second item

, , ,

30
Aug

The JavaScript Number object is a wrapper for numeric values. You can use it in combination with the new keyword and set it to a variable to be used later in JavaScript code:

var myNumber = new Number(numeric value);

Alternatively, you can create a Number object simply by setting a variable to a numeric value. This variable will then have access to the properties and methods available to the object.

 
 

In addition to storing numeric values, the Number object includes various properties and methods for manipulating or retrieving information about numbers. All of the properties available to the Number object are read-only constants, meaning that their values always remain the same and cannot be changed. Four properties are included in the Number object:

  • MAX_VALUE
  • MIN_VALUE
  • NEGATIVE_INFINITY
  • POSITIVE_INFINITY

The MAX_VALUE property returns the value 1.7976931348623157e+308, which is the largest number JavaScript can handle:

document.write(Number.MAX_VALUE);
// Result is: 1.7976931348623157e+308

Alternatively, using MIN_VALUE returns the value 5e-324, which is the smallest number possible in JavaScript:

document.write(Number.MIN_VALUE);
// Result is: 5e-324

NEGATIVE_INFINITY is the largest negative number JavaScript can handle, represented as -Infinity:

document.write(Number.NEGATIVE_INFINITY);
// Result is: -Infinity

The POSITIVE_INFINITY property is anything larger than MAX_VALUE and is represented as Infinity:

document.write(Number.POSITIVE_INFINITY);
// Result is: Infinity

The Number object also has methods that you can use to format or convert numeric values. The methods are:

  • toExponential
  • toFixed
  • toPrecision
  • toString
  • valueOf

     
     

Each method essentially does exactly as it is named—for example, the toExponential method returns a string representation of a number in exponential form. The uniqueness of each method is in the arguments it excepts. The toExponential method has an optional argument that can be used to set how many significant digits to use, the toFixed method determines the post-decimal precision based on the argument passed, and the toPrecision method determines the significant digits to display based on the argument passed.

 
 

Each object in JavaScript includes a toString and valueOf method. The toString method returns a string representation of a number (in this case), but in other objects, it returns a string representation of that object type. The valueOf method returns the primitive value of the object type that calls it—in this case, theNumber object.

 
 

The Number object alone might not seem terribly powerful, but it is an essential part of any programming language, and JavaScript is no exception. The JavaScript Number object provides the foundation for any mathematical procedures, which is essentially the foundation of all programming languages.

 
 

, , , , , , , , , , , , , ,

05
Aug

You can format numbers in javascript in following way:

var num = 12;

var dec = 2; // decimal part rounded to 2var fomatedNumber = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
alert(fomatedNumber) ;

num = 45.6739;

var fomatedNumber = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);

alert(fomatedNumber) ;

That’s it.

, , , , ,

03
Aug

The following Javascript function will reload webpage. You can copy this function and paste it in your webpage and just call the function. That’s it.

<script type="text/javascript">

function reload_page(){

var cur_url = unescape(window.location.pathname);
window.location.href = cur_url;

}

</script>

, , , , , , , ,

01
Aug
The following script will illustrate the use of child selector using jQuery:
 <html>
 <head>  
 <script src="http://code.jquery.com/jquery-latest.js"></script> </head>
 <body>
<ul class="main">    
<li>Product1</li>    
<li>Product2        
<ul> <li>Sub Product 1</li>
<li>Sub Product 2</li>
<li>Sub Product 3</li> </ul>    
</li>    
<li>Product 3</li> </ul>
<script>
jQuery("ul.main > li").css("border", "1px solid #999999");
</script>
</body>
</html>

, , ,

25
Jul

//Returns Only the File extension.

function get_file_extenstion($filename) {

if( !preg_match(‘/\./’, $filename) ) return ”;

return preg_replace(‘/^.*\./’, ”, $filename);

}

,

25
Jul

//Returns File Name stripping off the characters after hyphen in a file name.

function file_hyphen_strip($filename){

return preg_replace(‘/\-[^-]*$/’, ”, $filename);

}

, ,