Javascript: Object Detection or Browser Detection?

November 21st, 2009 by junal View Comments »

This question can be answered easily with “object detection” – why? Because some obscure browsers won’t be treated correctly and browsers that appear after you’ve written the page may not be adequately covered either as a result you will end up with some error in some browsers. Object detection works fairly in all browsers.

But, certain features of Javascript don’t work in all browsers. As an example “innerText” property doesn’t work in Firefox but in IE, Chrome Etc…this problem could be solved easily like the following ..

var text = x.innerText || x.textContent
//innerText for IE or other browsers and textContent for Firefox that works well.

Now, consider the following statement

Google Chrome doesn’t read encoding information that’s declared with document.write(). If you’re using this method to declare encoding in iframes, for example, you may see garbled characters when the iframe is rendered. Instead of:

document.write("<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">");
    ... other JavaScript code ...
</meta>

and when it might arrived? Let’s say you are using a Rich Text Editor where you will have onload events with document.write() and you will notice that iframe attribute (e.g name) value is not getting in the browser. Rather Chrome is setting name attrribute value from itself.

There is a tricky way to get it solved, in my case I used Firebug extension to get the attribute value and then detect the browser. Well in that case object detection doesn’t work for me. I must use browser detection right.

Let’s have a look at the following codes to get some idea about what I have been talking…

function notEmpty(){
 
        var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
        var textvalue = '';
        var framename = '';
        var elementValue = ''; 
 
        if(is_chrome) {
            elementValue = window.frames['Rich text editor'].document.body.innerText;
        }
        else if(navigator.appName =="Microsoft Internet Explorer") {
            elementValue = window.frames['myIframe'].document.body.innterText;
        }
        else {
            elementValue = window.frames['myIframe'].document.body.textContent;
        }
 
        if(elementValue =='')
        {
            //do what you want
            return false;
        }    
        return true;
    }

Conclution: It depends, generally it’s always good to use object detection no doubt. But when you have no option? Well then don’t wait to use browser detection eh… ;)

Check IFRAME Text by Javascript

November 20th, 2009 by junal View Comments »

Most of the Rich Text Editor use iframe, and besides server side validation it’s necessary to check client side validation with JS before storing any data. Let’s say you want to check if the body of iframe is empty. This is a little bit tricky, like you can easily check a textarea value with document.getElementById(idoftextarea).value; but it won’t work in case of iframe. Let’s consider the following scenario…

<iframe name="richEditorIframe" width="100%" height="25%" src="test1.txt">
</iframe>
 
<form name="formName">
     <textarea name="textarea" cols=80 rows=18 id="textArea">
         This is a test
     </textarea>
      <br />
      <button type="submit" value="Submit" />
</form>

Now, to check if iframe is getting in Database with empty value we can make a function like the following….

<script type="'text/javascript'">
    function checkEmpty(){
        var elementValue = window.frames['richEditorIframe'].document.body.innerText;
        if(elementValue=='')
        {
            //say something here
            return false;
        }
        return true;
    }
</script>

instead of innerText you can use innerHTML but then in most of the Text Editor it will show some HTML element which means you are not getting empty result even if you didn’t write anything. So, innerText will give you the iframe body value if it’s written. Now let’s call this function onSubmit in the form…

<form name="formName" onSubmit="return checkEmpty();">
      <textarea name="textarea" cols=80 rows=18 id="textArea">
          This is a test
       </textarea>
       <br />
      <button type="submit" value="Submit" />
</form>

That’s it, now it’s simple :)

Welcoming All Bangladeshis to “Amra Positive”

November 8th, 2009 by junal View Comments »

I promised to release “Amra Positive” soon, today i’m announcing the release of this small application that is developed for Bangladeshis. Let me give you some ideas about the application first…. [i'm just coping the “about” page from the application]

“amra positive” is a place for Bangladeshis to leave good and positive news about Bangladesh, friends, family, events etc. no pressure, but it’s up to you to leave something good behind within 150 characters. there is no moderator to modulate your posts in this application. if you don’t like to post or don’t have anything to say, you can still vote on others; posts. so let’s have a look what you can do here…

  • you can post a message about the country, your friends or family, events that you liked or think is positive

  • you can vote on your own posts and others’ posts. currently there is no commenting system. you can only vote positive or negative about a post.

  • you can tweet a post if you like. links will be automatically shortened, you don’t have to worry about that.

  • you can check posts that were voted the most positive from users from “top10″ tab

  • as an owner of the post, you have access to delete it

  • you can check all your past/previous posts by clicking the “authors” tab or by clicking your nick name

  • clicking on a message will show that specific post in an individual page

  • you have to use your gmail id to sign in. please note: that password is not shared with the admin of this application, only the gmail id is shared. so there is nothing to worry about

  • all text in this site in lowercase to make it look a little bit more different.

    Home page of Amra Positive

    Home page of Amra Positive

in addition, you can subscribe to the most recent and top 10 posts by clicking on rss button. there is currently no feedback system right now. but if you have any feedback, comments, suggestions or concerns just email me at junal@junalontherun.com

p.s : this is a beta version of this application. looking forward to hearing from you. report any bugs in the application and let me know whatever is on your mind by email. thanks for your patience and support with this idea!

Link : http://amrapositive.appspot.com/

Quick CSS Hack for IE 7/8

November 7th, 2009 by junal View Comments »

As a developer you know how painful this IE shit is. If you are required to change a large amount of css classes just for IE 7/8 then it’s better to create a file with ie7.css or ie8.css and include the css file in your application. That will overwrite all classes for your IE 7/8 only and well don’t forget to keep “!important” to get the priority over existing properties. 

But, what if you just want to change only one or 2 properties just for IE? 
As an example let’s say you want to keep height of a DIV for IE8 in “auto” but for other browsers you want to keep it like height:10px; this is how we can do it…

.something {

    height:10px;  /* will work for rest of the browsers except IE8*/
    height /***/: auto9;   /* this will just change the IE8*/
 }

And what about IE7 ? just modifying the above class 
.something {
    height:10px;  /* will work for rest of the browsers except IE7*/
    *height: auto   /* this will just change the IE7*/
 }

Cool! i liked to get these hacks from online, certainly not a very good practice but hey, it’s not bad to use some hack instead of getting lost in nowhere? 

“Amra Positive” is Coming Soon…

November 3rd, 2009 by junal View Comments »

Story behind this work was a little bit different. I wanted to learn python and thought best way to learn something new would be doing something practical with it. So decided to develop this application called “Amra Positive”. 

What is Amra Positive?
A small application that is being developed for Bangladesh community. 
And what it does? 
Amra positive is a home for people of Bangladesh who are hopeful about this country, about their life, about their friends/events…etc. Who is tired of all bad news and really in need of some positive and good news. I hope this name is enough self explanatory but still let me tell you as an user what you can do with it….
  • If you have any good news about the country, friends, family, events pay a visit in this app and sing in with gmail id and then let the world know. 
  • Share your hopes and dreams within 150 characters 
  • If you don’t even like to post anything, you still can vote on others’ post to get them on top list news 
  • And well, tired of hearing bad news about everything that is going around ? Visit here, because this is the home of all good and positive stuff about you! 
is there any moderators of your posts? 
No! not for now…nobody is going to moderate your post. Personally i will leave this judge on you. 
But what the hell, where is the link of this application? 
It’s ready and coming “soon”! Soon means soon….i promise it will come live pretty soon. Just keep your eyes on here in this blog and of course you will have more clear ideas once it goes live. 
Thanks for your patience….

Get Adobe Flash playerPlugin by wpburn.com wordpress themes