Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

2/13/2014

Javascript Error Handling(Try, Catch)

When you want to handle errors in your code, you can use try, catch statement.
Here is the tips for the way to switch error types using error object types.

Errors Type

  • EvalError: using eval wrongly
  • RangeError: number or array range violation
  • ReferenceError: wring reference used
  • SyntaxError: worng syntax in your code
  • TypeError: varialble used with different type
  • URIError: encodeURI, decodeURI missused
Example
try{
//some code here
}catch(e){
   if( e instanceof TypeError){
          //do somethign for error
   }
}

9/17/2008

Injection Javascript with innerHTML

When you create 'javascript' dynamically in IE, you need to work with HTMLDom Element.
This is the way simple way to insert 'Javascript' in IE, using [element].innerHTML.

According to MSDN, there is the attribute 'defer' for Script Element supporting inline script available when element inserted. By the way there is a small problem, IE recognizes script element as source for innerHTML only after Text element.

This is the example.

var container = document.getElementById("id");
var resultDocument = "<script defer='true' >.......</script>";
var dummyTag="<div style='display:none;position:absolute;top:0px;left:0px;'> </div>";
container.innerHTML = dummyTag+resultDocument;

This works for only inline script, if you want to insert script source, create script element and insert into 'Head  Element'


Enjoy 'javascript' ~~~~