Wednesday, January 22, 2014

Screen Width and Height for iPhone, iPad, Android Tablets and Smartphones Using Javascript


When it comes to screen width and height, it is a bit tricky for tablet devices as the width and height vary according to the orientation of the tablet.

Therefore it is crucial to detect the screen dimension for websites that are sensitive to the width and height of the screen such as the sticky notes on the web page. The sticky note usually has to be accurate on the location on the web page or it may block important information in the web site.





The following are the Javascript codes that I used:

<script>

var clientWidth = document.getElementsByTagName("body")[0].clientWidth;
var clientHeight = document.getElementsByTagName("body")[0].clientHeight;
.
.
.
</script>


However, there is a problem here. The width and height will change whenever the user changes the orientation of the tablet. Here are improved codes after taking such scenario into consideration:

<script>

function checkDimension() {
   var clientWidth = document.getElementsByTagName("body")[0].clientWidth;
   var clientHeight = document.getElementsByTagName("body")[0].clientHeight;
.
.
.
} 
</script>

<body onresize="checkDimension()"> .
.
.



Whenever the tablet changes the orientation, the onresize event on the browser will go to checkDimension() function. In that case, the Javascript is able to react to any changes to screen size and orientation.

Read More »

Monday, January 20, 2014

Strange Tablet (Touch Device) Problem with Javascript's MarginLeft/MarginRight DOM property


This is really hard to explain. If you do your coding in Javascript to move a DIV/SPAN manipulating the value in marginLeft/marginRight DOM property, you will run into a problem that your touch devices, ie., iPad, Android tablet/smartphones won't work with this trick if you have a align=center property somewhere before the DIV/SPAN.

Foe example:

.
.
.

<table><tr><td align=center><div id=moveDiv>...</div></td></tr></table>

<script>
document.getElementById('moveDiv').style.marginLeft = "100px";
</script>


The above code will work on any desktop browser but won't work on any touch capable devices such iOS/Android tablet or smartphone. Strange? Of course, when you have the align=center statement removed, things will work flawlessly in any device.

Read More »

Preload Images using Javascript


There are times that you need to preload images on your web page to avoid the long load time required for a dynamic HTML site.

I've tried many examples found on the web but none of them are really working on today's browsers.

Here's my way of making this happen:

<script>

var image_loc = new Array('/image/pic1.jpg','/image/pic5.jpg','/image/pic13.jpg','/image/pic9.jpg');
var elem;

for (i = 0; i < image_loc.length; i++) {
   elem = document.createElement("span");
   elem.id = "preload_img"+i; // this is optional
   elem.style.position = 'absolute';
   elem.style.visibility = 'hidden';
   elem.style.left = '0px';
   elem.style.top = '0px';
   elem.style.width = '10px'; // this can be bigger as long as it is not wider than the screen to avoid extra scroll bar appears at the bottom of the window
   elem.style.height = '10px';
   elem.style.background = "url('"+image_loc[i]+"') no-repeat left top";
   document.body.appendChild(elem);
}

</script>


The trick is to create hidden elements and load the images to be preloaded into background. Those images will be hidden and located at the upper left corner of the screen. The location and size of the SPAN element can be of other values. Hope you like my first new trick in 2014!

Read More »

Saturday, September 7, 2013

Latest Method to Change Facebook Like Box Border Color

As of today, this method still works. This may not work in the future as Facebook changes its support every now and then.

Anyway, here are the codes:


The CSS:
.fb-like-box {
  width: 348px;
  height: 248px;
  overflow: hidden;
  position: relative;
  border: 1px solid #dddddd;
}

.fb-like-box .dummyBox {
  margin: -1px 0 0 -1px;
  border: 0px;
}


The HTML:
<div class="fb-like-box">
<div class="dummyBox">
<fb:like-box width="350" height="250" colorscheme="light" show_faces="true" stream="false" header="false"><iframe width="350px" height="250px" frameborder="0" allowtransparency="true" scrolling="no" title="fb:like_box Facebook Social Plugin" src="http://www.facebook.com/plugins/like_box.php?color_scheme=light&amp;header=false&amp;href=http%3A%2F%2Fwww.facebook.com%2Fpages%2F[Your FB Page Path]&amp;locale=en_US&amp;show_faces=true&amp;stream=false&amp;width=350&amp;height=250" style="border: none; visibility: visible; width: 350px; height: 250px;"></iframe>
</div>
</div>



Those in red are the ones you might want to change according to your preference. Those in green are the important settings to pay special attention to and also the locations where the magic comes from. Basically it is to enclose the FB iframe with two DIVs. One is to enclose to FB iframe with purposely 1 pixel smaller on each side to cover up the original FB like box border. The 2nd DIV is to shift the other DIV to upper left hand corder by 1 pixel using "margin: -1px 0 0 -1px;".

Hope you like this little trick. Enjoy!

Read More »

Friday, September 6, 2013

Javascript to Sniff Android Based Browsers


Updated on 7th of February, 2014: The is_touch_device() function in the following is no longer working. Please check out my latest is_touch_device() function that works in the latest Chrome browser (Nevertheless, the rest of the codes are still working. Just replace the latest is_touch_device() function will do):

http://webtrick101.blogspot.com/2014/02/detecting-browsers-touch-capability.html



Using Javascript to sniff out Android based broswer is tough as the list of Android devices is very lengthy. Here's my Javascript way of performing the Android sniffing task.

<script>

function is_touch_device() {
 try {
  document.createEvent("TouchEvent");
  return true;
 }
 catch (e) {
  return false;
 }
}

var ie = (navigator.userAgent.indexOf("MSIE") != -1);
var ipad = (navigator.userAgent.indexOf("iPad") != -1);
var iphone = (navigator.userAgent.indexOf("iPhone") != -1);
var bb = (navigator.userAgent.indexOf("BlackBerry") != -1);
var touch = is_touch_device();
var android = touch && !ipad && !iphone && !bb && !ie;

if (android) {
 alert("Hello Droid!");
}

</script>


The logic? A touch capable device which is not either iPad, iPhone, Blackberry, or Windows 8 (touch screen), chance is very high that it is an Android device. But the trend will keep on changing. This should be still good for now.

Read More »

Setting Cookie via Javascript Not Working for my Android 2.33 Gingerbread Device

I believe the current Android based device won't have this issue. But it happened to my antique Android Gingerbread based phone. After plenty of testings, here is my conclusion:

Javascript with Problem:
document.cookie = "apple = orange;";


Javascript that worked on my phone:
document.cookie = "apple=orange;";



Very unexpected problem. Simple and unexpected solution as well.

Now I kind of like this name of Android version: Froyo (2.2–2.2.3) if you know what I mean. :)

Read More »

Thursday, September 5, 2013

When DOM marginLeft property is not working in Mobile tablets such as iPad and Nexus 7...

I had this problem and I was unable to get it work until several hours of debugging:

Javascript:
document.getElementById("myDiv").style.marginLeft = "100px";


HTML:
<table><tr><td align=center><div id="myDiv"> . . . </div></td></tr></table>


Guess what? It was due to the very unexpected "align=center" that prevents the div beneath from moving anywhere.

Weird problem. Weird cause.

Read More »

Tuesday, August 6, 2013

Apostrophe (') Gets Censored After Inserting Into MySQL. How to Fix it?

I think MySQL doesn't like apostrophe or '. When inserting into MySQL, the apostrophe goes missing. How to fix this? Simple. Here's it is:

$text = "My car's mirror is broken!";
$text2DB = addslashes($text);
mysql_query("INSERT INTO ... $text2DB ... // complete the MySQL INSERT INTO action here



PHP's addslashes() function is enough to solve this problem. It will add slashes when necessary automatically. Isn't that neat?

Read More »