Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Monday, August 28, 2017

Javascript to Limit HTML Width for Mobile Devices of Any Orientation

The viewport parameter in the HTML Meta Tag is used to limit the screen width. Another thing to pay attention for these codes is the neccessity to differentiate between tablets and phones. Phones tend to have less resolution whereas tablets can have resolutions comparable to that of PC or Mac. I use a simple criteria to differentiate the two by checking the screen width whether it is wider than 700 pixels. If so, tablets' width will be forced to limit to only 700 pixels. For non-tablets, the width will be limited to the native screen width found in the Javascript system parameter screen.width. Pay attention to when orientation changes. Android will have the width and height paramters swapped whereas the mobile Safari will have the same width and height irregardless of the orientation.

The <body> onorientationchange is used to listen to the orientation change even. Whenever it is fired, the 'content' meta will be reconfigured according to the orientation status. Javascript's window.orientation is used to check the current orientation status in terms of degrees.

Let's jump into the codes:

<html>
<head>
<title>Javascript to Limit HTML Width for Mobile Devices of Any Orientation</title>

<script>

var mobile = navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/iPhone|iPad|iPod/i) || navigator.userAgent.match(/Opera Mini/i) || navigator.userAgent.match(/IEMobile/i);

var safari = navigator.userAgent.match(/iPhone|iPad|iPod/i) && navigator.userAgent.match(/Safari/i);

var ot = ""; // variable "orientation" is used by mobile browsers
if (safari) {
 if (window.orientation == 0) { ot="portrait"; } else { ot="landscape"; }
}
else {
 if (screen.width<screen.height) { ot="portrait"; } else { ot="landscape"; }
}


if (mobile) {
 var mobileWidth = getMobileWidth();
 alert("setting the width of "+mobileWidth);
 document.write('<meta id="vp" name="viewport" content="width = '+mobileWidth+', initial-scale=1.0">');
}
// if it is not mobile, viewport is not supported or will be ignored

function getMobileWidth() {
 var width;
 var isTablet = 0;
 if (safari) { // safari screen width and height remain constant irregradless of the orientation
  if (ot == "portrait") { width = screen.width; if (width>400) {isTablet=1;} }
  else { width = screen.height; if (width>700) {isTablet=1;} }
 }
 else {
  width = screen.width;
  if (ot == "portrait") { if (width>400) {isTablet=1;} }
  else { if (width>700) { isTablet=1;} }
 }
 if (isTablet) {
  if (ot == "portrait") {
   return 400;
  }
  else {
   return 700;
  }
 }
 else {
  return width;
 }
}

function changeViewPort() { //change viewport dynamically by changing the meta 'content' attribute

 if (safari) { // mobile safari will not swap screen.width with screen.height after orientation change
  if (window.orientation == 0) { ot="portrait"; } else { ot="landscape"; }
 }
 else { // other broswers will have height and width swapped
  if (screen.width<screen.height) { ot="portrait"; } else { ot="landscape"; }
 }

 var mobileWidth = getMobileWidth();
 alert("setting the width of "+mobileWidth);
 alert("screen in "+ot+" mode w:"+screen.width+" h:"+screen.height+" -->"+window.orientation);

 if (mobile) {
  var vp = document.getElementById('vp');
  vp.setAttribute('content','width='+mobileWidth+', initial-scale=1.0');
 }
}

</script>

</head>

<body bgcolor=orange onorientationchange="changeViewPort()">

<table width=100% height=100% align=center valign=middle>
<tr><td width=100% height=100% align=center valign=middle style="font-family: Lucida Sans, Lucida Sans Unicode, Helvetica, Arial, sans-serif;">Under Construction</td></tr>
</table>

</body>

</html>


Please note the meta tag has been given an ID (id="vp") so that it can be modified when necessary. onorientationchange is used instead of "onresize" as "onresize" will make give a lots of false signal even when the address bar in mobile browsers shows up or hides.

Read More »

Wednesday, February 24, 2016

Good Looking HTML Table Style


Creating a good looking HTML table sounds easy but when it comes to actual execution, it is not as easy as it seems. Consider the following most basic HTML textbook codes for a 3x3 HTML table:

<table border=1>
<tr><td>Apple</td><td>Book</td><td>Car</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
</table>

Here is how it looks:



This basic table looks unprofessional at the first glance. How about making it with less or thinner border?


<table border=0 cellspacing=0 cellpadding=0>

<tr><td style="border:1px solid #aaa">Apple</td><td style="border:1px solid #aaa">Book</td><td style="border:1px solid #aaa">Car</td></tr>
<tr><td style="border:1px solid #aaa">1</td><td style="border:1px solid #aaa">2</td><td style="border:1px solid #aaa">3</td></tr>
<tr><td style="border:1px solid #aaa">4</td><td style="border:1px solid #aaa">5</td><td style="border:1px solid #aaa">6</td></tr>

</table>

Here it the screen capture of how it looks again:



By disabling the border for entire table and enabling border styling for each individual cell (highlighted in green), the border looks thinner now. But the problem now is the different thickness for the lines. The lines within the box seems thicker than those on the far out. This is due to the double border problem. Those with thicker line came from the neighboring cell borders. Now the next example will demonstrate how to eliminate the redundant lines using CSS:

<table border=0 cellspacing=0 cellpadding=0 style="border-collapse: collapse;">

<tr><td style="border:1px solid #aaa">Apple</td><td style="border:1px solid #aaa">Book</td><td style="border:1px solid #aaa">Car</td></tr>
<tr><td style="border:1px solid #aaa">1</td><td style="border:1px solid #aaa">2</td><td style="border:1px solid #aaa">3</td></tr>
<tr><td style="border:1px solid #aaa">4</td><td style="border:1px solid #aaa">5</td><td style="border:1px solid #aaa">6</td></tr>

</table>

Finally, this looks good:



Setting CSS border-collapse property to collapse now solved the problem. The problem now is the huge number of codes just for a simple 3x3 table. The codes will look complicated when it comes to large table. Now the CSS styling comes into play to simply the coding:

<html>
<style type="text/css">
.s1 {font-family: arial; font-size: 13px; padding: 12px; border-collapse: collapse; }
.s1 td { padding: 5px; border: 1px solid #aaa; text-align: center; }
</style>


<body>

<table class=s1>
<tr><td>Apple</td><td>Book</td><td>Car</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
</table>

</body>
</html>

It now looks more prefessional:



Now the table looks much better. Please note that those in green are the extra CSS styles to apply spacing, alignment and font to the table. With these extra settings, the table looks even better! Now you have it, enjoy!
Read More »

Tuesday, December 29, 2015

Large HTML Input Text and Space Suitable for Mobile Device Web Browsing


With the popularity of tablets taking the center stage ever since the launch of the first iPad, web pages were being swayed towards simplicity and user-friendliness. The smartphone revolution then followed suit and mobile web browsing had then become a norm until today. As the screens of tablets and smartphones are a lot smaller than deskstops, web pages are being made simpler with the texts bigger to suit the smaller screens.

The obvious change to the web page is the login page. The login input text and space are now bigger. Here is an example of the HTML and CSS codes I use to follow this trend:

<html>

<table height=100% width=100% align=center valign=middle><tr><td>
<table width=520 height=160 align=center valign=middle style="border: solid #cccccc 1px; background-color: #ddeeff">

<tr><td width=20>&nbsp;</td><td align=right><font face=arial size=3 color=#888888>Login ID</td><td width=20><br></td><td><input type=text name=lg style="height: 40px; padding: 10px 0px 10px 8px;font-size:16px" maxlength=40 size=40></td></tr>

<tr><td width=20>&nbsp;</td><td align=right><font face=arial size=3 color=#888888>Password</td><td width=20><br></td><td><input type=password name=pw style="height: 40px; padding: 10px 0px 10px 8px;font-size:16px" maxlength=40 size=40></td></tr>

<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" value="Log On Now!"></td></tr>

</table>
</td></tr>
</table>

</html>


The key to this effect is hightlighted in green. The style in CSS is important to make this work. The "height: 40px;" CSS is important to get this to work on IE browsers.

Here is the screenshot after running the HTML in Chrome:



Enjoy!
Read More »

Thursday, December 18, 2014

Latest Facebook Share Method for HTML (Dec 2014)

The Facebook share method has gone through a lots of changes since my last post long ago. Here is the latest update. Please note that these codes may not be working by the time you use them as Facebook updates its coding requirements very often.

Anyway, here are the codes:

<html>
<head>
<meta name="description" content="Welcome to my website! My phone number is 1234567! Thank you!">
<meta property="og:title" content="Your Facebook Post Title" />
<meta property="og:type" content="website" />
<meta property="og:image" content="http://yourdomainname.com/pic1.jpg" />
<meta property="og:url" content="http://yourdomainname.com" />
</head>
<body>

<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>

<div id="share">
<fb:share-button class="meta">
<link rel="image_src" href="http://yourdomainname.com" />
<link rel="target_url" href="http://yourdomainname.com" />
</fb:share-button>
</div>

</body>
</html>


Note: Please note that you need to own the website in order to place these codes in the index page. If you don't own the website or domain, the share information will not show up correctly. Secondly, if you run these codes on the "localhost", the share button won't show up. Please note that you need to change those in blue to your own information.

If the information doesn't show up correctly or you want to update your Facebook share information, you need to use Facebook linter or debugger. Here's the link: https://developers.facebook.com/tools/debug/. The linter/debugger link has also changed since my last post. Please also note that you may need to press "Fetch new scrape information" a few times for the system to update.

Read More »

Thursday, August 14, 2014

How to Quickly Identify Missing/Extra HTML Tag

When your HTML gets more and more complicated and packed, a slight mistake in the tagging will render you helpless. It could take a long time to find just a missing or extra tag. My way of solving this problem is to use Notepad++! It's free but it is only available on Windows.


It's easy. You just need to click on the tag and look for missing PURPLE! Once you click on any of the HTML tag, the corresponding closing/opening tag will be highlighted in purple. If you click on a tag and nothing happens, that means Notepad++ is unable to find a corresponding tag for it. You've found the problematic tag! There are still chances that the highlighted tag in purple is wrong. That means you need to also double check the highlighted tag to see whether they are coded the way you really intended.

The screen capture shows the tag highlighted in purple after you click on it. There you have it and happy debugging!

Read More »

Tuesday, August 6, 2013

From Your Check Out Form to Paypal's Note to Seller


If you want to add note to the Paypal seller from your check out form or shopping cart, you need to add the cn input name and value in the post form before submitting to Paypal. Something look like:

<input type=hidden name=cn value="Please ship directly to my dorm room">


If everything is done correctly, the "note to seller" will appear in the Paypal check out form.
Read More »

Tuesday, August 21, 2012

Simple HTML/Javascript Codes to Display your Live Mouse Coordinates (Firefox Fix)

The firefox friendly version:


<html>
<head>

<script type="text/javascript">

var ff = (navigator.userAgent.indexOf("Firefox") != -1);

function showXY(e)
{
  if (ff) {
     var s = 'X=' + e.pageX + ' Y=' + e.pageY;
     document.getElementById('coordinates').textContent = s;
  }
  else {
     var s = 'X=' + window.event.clientX + ' Y=' + window.event.clientY;
     document.getElementById('coordinates').innerText = s;
  }
}

document.onmousemove = showXY;

</script>
</head>

<body>

<div id="coordinates"></div>

</body>
</html>


Here's the firefox friendly version. Instead of using onmousemove event capture in the body tag, it is initiated in javascript before the body. IF you initiate the event in the body tag, it won't work. A rare Firefox problem with a weird solution. But it works.
Read More »

Monday, August 20, 2012

Simple HTML/Javascript Codes to Display your Live Mouse Coordinates

Here they are:


<html>
<head>

<script type="text/javascript">

function showXY()
{
   var s = 'X=' + window.event.clientX + ' Y=' + window.event.clientY;
   document.getElementById('coordinates').innerText = s;
}

</script>
</head>

<body onmousemove=showXY()>

<div id="coordinates"></div>

</body>
</html>


The key component for this to work is highlighted in green. However this won't work in Firefox. The firefox friendly version can be found here. Sorry I wasn't aware of the Firefox problem earlier. Another way to fix this is to use JQuery.
Read More »

Wednesday, May 25, 2011

My Best Way to Place favicon.ico for All Browsers Type

Favorite Icon (favicon.ico) can help boost the professional look of your website.

But somehow different browsers support it differently. Here are the codes I use for maximum compatibility:


<head>
<link rel="icon" href="/favicon48.ico" type="image/ico"> <!-- for Mozilla based browsers -->
<meta name="msapplication-task" content="name=YOUR_ICON_NAME;action-uri=./favicon32.ico"> <!-- for IE9 -->
<link rel="SHORTCUT ICON" href="./favicon32.ico" type="image/x-icon"> <!-- for IE8 -->
</head>


Besides you also need to place a 16x16 small icon named favicon.ico in the root path of your server to be compatible with IE5/6.

Size? I would say make each of those sizes: 16x16, 24x24, 32x32, 48x48, ... just in case future IEs can use them.

What software to make them? I use Photoshop with plugin to export .ico file.
Read More »

Friday, March 12, 2010

IE Text <INPUT> Minimum Width Problem

If you happen to specify the “size” in the text input form using <input> which is less than 10, chances are the size will be “fixed” to around 10 even if you specify a lower number. In order to solve this peculiar problem of IE or maybe Firefox or Chrome, I have tried the following CSS declaration and it works:

table-layout: fixed;

Strangely, only Safari works well without this fix. Firefox and Chrome can’t even work well using this fix. But at least IE doesn’t have this problem after this fix.
Read More »

Tuesday, March 9, 2010

Redirect a URL inside a frame to a _top page

It’s easy to redirect a URL if you don’t care whether you are from inside a frame or to its own frame using META redirection or window.location javascript redirection.
What if your HTML is inside a frame calling to redirect itself to a _top page? Tricky?

I use this:

if (document.referrer.indexOf('YourAliasName') != -1) {
   top.location.href='http://www.YourMainDomainName.com';
}

You can substitute YourAliasName with a portion of your domain alias name so that the redirection won’t go into infinite loop.

I needed this because my hosting company enclosed my domain alias inside a frame and I need to redirect the domain alias to the main domain name so that it is like a normal redirection without being redirected back inside the frame. Therefore the code above that is highlighted in red is important to move the redirected page to the _top page level.
Read More »

Monday, February 15, 2010

PERL Trick to Make Alternative Row Color

The mod or modular operator in PERL (%) is responsible for this trick.

The following codes show how I did it:


.
.
.
$total_hits++;

if ($total_hits % 2) { $rowcolor = " bgcolor=#FFEEEE"; }
else { $rowcolor = " bgcolor=#FFFFFF"; }
.
.
.


These codes are in a loop to list out all the items. You can place the $rowcolor in the <TR> tag. The rows displayed will be in pink and white alternatively.
Read More »

Sunday, February 14, 2010

The Maximum Length of a Cookie Name

I once encountered a problem with the cookie length with Firefox when the cookie name or value is too long.

I now try to imitate the same problem I encountered last time, the problem is gone.

But I use shorter cookie name and value so that Firefox won't reject them I encountered last time which caused me a lots of pain debugging.

It's good to test cookies using firefox as one can view the cookies easily using this:

Tools->Options...->Privacy->Show Cookies

Thank goodness there is a browser called Firefox or the debugging of cookie problem will be quite troublesome.
Read More »

Thursday, February 4, 2010

All the Pre-selections and Pre-checks in HTML Selection ie. OPTION and INPUT(Radio+Checkbox)

For Pull-Down Selection:

<select name=country>
<option>Argentina</option>
<option selected>Australia</option>
<option>Austria</option>
<option>Bahamas</option>
.
.
.
</select>


For Radio Selection:

Orange <input type=radio name=type value=apple checked> Apple <input type=radio name=type value=orange>


For Checkbox Selection:

<input type=checkbox name=type checked>



Coming next, how to detect those selections in PERL-CGI script...
Read More »

Wednesday, February 3, 2010

Weird Image Alignment Problem in HTML

Ever encounter this problem?



The alignment of your image and the adjacent text seem hard to be aligned. The text is always lower than the lowest part of the image. No matter how you adjust the CSS of the <img>, nothing works.

There is a simple solution to this:

<Table><TR><TD><img src="/image/but1.gif"></TD><TD><a href="[http_address]">Click if you need to click</a></TD></TR></Table>

Changed from the common mistake of mixing image with text next to each other:

<img src="/image/but1.gif"><a href="[http_address]"> Click if you need to click</a>

All you need to do is just to make a table out of it. Separate the image and text and put them into separate table data columns. Problem solved. If you need to further align the text, you can always use valign to adjust the vertical alignment of the text. Anyway, the TD height adjustment won't help as the TDs share the same height unless you create another table inside the TD.



Simple problem. Simple solution but hard to catch.
Read More »

Tuesday, February 2, 2010

How to fix HTML table problem?

This is the most common problem that a web developer faces throughout his or her career. It is the table problem. Why is the text not aligned? Why is the image glued to the other end instead being centered? These are the common questions in mind especially when you have many layers of tables stacked up to each other.

There is a easy fix to this. COLOR IT!

But RULE#1 before this. Make a backup copy. When you have a copy backed up, you dare to do any experiment with it. Usually I will purposely do these:

<table bgcolor=red>...
or
<tr bgcolor=green>...
or
<td bgcolor=orange>...


You will see very clearly how your table or table row or table data behave in the browser. I usually run these experiments with IE, Firefox as well as Safari. However, it also depends on your DOCTYPE. Different DOCTYPE has different specification.

COLOR it! My friend. This is crucial especially when you are playing with WIDTH=100% game.
Read More »

Sunday, January 31, 2010

How to mix two DOCTYPE in one website?

Simple, just make one of them in <IFRAME>. For example, you need these two types in one web page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

AND

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


Which one to be in IFRAME and which one to be in main frame?

It is recommended to make the one with looser requirement in the main frame. I would pick the HTML 4.01 one in my main frame. How about you?
Read More »

Saturday, January 30, 2010

How do you remove/force the underline in link anchor <A>?

To remove:

Use CSS. Simply use the following CSS codes:

a {text-decoration:none;}

or directly:

<a href="[link destination]" style="text-decoration:none;">link</a>



To force:

<a href="[link destination]" style="text-decoration:underline;">link</a>

or

<a href="[link destination]"><u>link</u></a>



To show underline only if the mouse is over the top:

CSS method:
a {text-decoration:none;}
a:hover {text-decoration:underline;}



Hope you like these simple ones. Beginners should like these tricks.


Read More »

Wednesday, January 27, 2010

Fastest Way to Get The Coordinates For Usemap

As we all know that we need to specify the coordinates when we set up the Usemap for our image.

I recommend using Photoshop.

Launch Photoshop (I still use version 7 in 2010), pull down Edit->Preferences->Units & Rulers. Set the rulers setting to pixels.

Open your image file. The use the selection tool to select the upper left corner like in the following:



Then go to the navigator toolbar window (usually on the upper right hand corner):




Click on the info tab and you will see:





The coordinates are here! Are they neat?

Repeat the same steps on the lower right corner of your image to get the coordinates.


Walla! Plug those numbers into your usemap. You can find the basic of usemap using Google easily.



Read More »

Sunday, January 24, 2010

How do you control the height of a table?

For those who tried to adjust the definite height of a table know that it is hard to control the height of a table that looks similar in all the browsers.


Here are the codes that I used to make sure the height is showed as defined:

<table cellpadding=0 cellspacing=0 border=0>
<tr><td height=13><img src="img/dummy.gif" height=13 width=1></td></tr>
</table>


You also need to prepare a dummy gif file to make this happen. The dummy gif file is better to be transparent in case you have background image from the upper tables and you don't this dummy gif to block it. The width is optional if your gif file has the smallest size of 1x1.

For a height of 13, it is not achievable by just specifying it in the <TD>. This is the only way I can think of and it comes in handy when you want to make your web design go according as planned in your photoshop design.
Read More »