Pages

Saturday, April 19, 2014

How to prevent from declaring global variables in JavaScript?

Introduction

When we declare a variable in JavaScript; if its declared with the var keyword it will be a local variable and if we omit the var key work it will be implicitly declared as the global variable. Developers are always careful on creating global variables.

Problem Background

Developers may accidentally declare global variable without their knowledge.

Solution

  1.  
  2. function funcationName() {
  3.      "use strict";
  4. };


To avoid developers from creating global variable you can use strict mode as given below. .

 
Conclusion

If we use strict method; when we debug we will get an exception on the line where we have declared global variables. See the screenshot below.

Wednesday, April 16, 2014

The property 'opacity' is not compatible with Internet Explorer 8.0

Introduction

I had to set opacity value for a text using cascading style (CSS). But when I type opacity it showed “The property 'opacity' is not compatible with Internet Explorer 8.0” in Visual Studio. See the screen shot below. 



Solution

To set the opacity value you have to set both opacity and a filter value; so that it will work for all browsers. 

   
p {
    opacity: 0.8;//0.0 is fully transparent and 1.0 is fully opaque 
    filter: alpha(opacity=80);//Here 0 is transparent and 100 is fully opaque 
}
 

Conclusion

Reference: 
Programming with HTML5 with JavaScript and CSS3 – A Microsoft Learning Product

Wednesday, April 9, 2014

Redirect to an external website from SharePoint App

Introduction

I’m assigned to a project where I’m to develop SharePoint hosted app using JSOM.

Problem Background

There I wanted to redirect to external web page. Eg.http://google.com. I used 

  • location.href='http://google.com'; 
  • window.location.replace("http://google.com");
  • document.location.href = url;
which we use normally to redirect to another website. But they didn’t work for me. This is because there was a post back on my page and I still want it.

Solution


I set a Timeout and it worked for me; use the below code to redirect.


   
  setTimeout(function () { document.location.href = url }, 20);

Conclusion


Hope this will help you. :)