Posts Tagged ‘html’

How To Submit A Form To Itself On Enter Without Refresh

October 12, 2009

I’m not sure if many people would want to do this or if good reasons exist for doing this; however, I wanted to submit a form to itself without page refresh for a trivia game.  So, after taking the time to research and figure it out, here is a solution.

This solution does not actually submit the form; rather, it uses two JavaScript functions to simulate submitting a form.  By simulating a submission of the form rather than actually submitting the form, one can avoid a page refresh. 

The first step to this process is calling a couple of JavaScript functions in the input element or another element of the form. Below is what my form element looked like for my game.

<form>
Team Name: <input id="txt1" onkeypress="return disableEnterKey(event)" onkeyup="showHint(this.value, event)" type="text" />
</form> 

It may be possible to achieve the same goal by just using onkeypress=”return disableEnterKey(event)”; however, this didn’t seem to work for me. Thus, I added event as a second argument to the showHint() function (the function which does most of the work in my game), and doing this made it possible for the function to handle the form data on enter as well.

The next step is coding these two functions. Below is the disableEnterKey() function that I used to disable form submission on enter.

function disableEnterKey(e)
{
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
return (key != 13);
}

I grabbed this function off of www.bloggingdeveloper.com. This code should be included in the head section of your html file, or you can include it in a separate .js file and link to it in the head section of your html file as I did.

I included a similar block of code in my showHint() function to process the form data when the enter key is released.

var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if(key==13)//the enter key was pressed
{
//your code to run when the enter key is pressed
}

That pretty much sums up this tutorial. If you would like to see what the working example functions like, you can visit my two minute drill game. It is a game to see if you can name all 32 NFL teams in two minutes. If you play, you should be aware that you are responsible for remembering the teams that you already named, and there is no special prize or notification if you name all 32 teams in the two minute time limit.

3 Ways to Include One HTML File in another HTML File

October 5, 2009

Sometimes it makes sense to create one html file that will be used in many other html files.  For instance, you might have some code that you would like to use as a header on each page of your web site.  Instead of typing this code out on each page of your site, you could just create a separate file with this code and name it header.html. Also, header.html doesn’t have to contain a complete html file structure with <html> </html>, <head> </head> and <body> </body> elements; rather, it just needs to follow the html standards as part of the file in which it will be included. In example, you could create a file called heading.html with the following contents.

<body>
<h1> Welcome to my site </h1>

Then, type the include statement just after the head element in the file that you wish to include heading.html in, and there will be no problems rendering the site. 

Now, I will describe three ways to include this file named heading.html. The first method will use php (Note: use must have php installed and enabled on your server for this to work).  To include this file using php, change the name of the file from heading.html to heading.php. Also, the file that has the include statement must also be saved with the .php extension.  Finally, at the location where you would like the code to appear type:

<?php include('heading.php'); ?>

The next two methods are virtually the same;  they just involve using different file extensions.  As noted before, you should make sure these technologies are enabled on your server before using them on your site. In the next to methods of performing a server side include, if you want to include a file called heading.html, this file can keep the .html file extension. Then, the file that you would like to include heading.html in would need to be saved with a .asp or .shtml file extension. The code for the include statement is below.

<!--#include file="heading.html" -->

In conclusion, you now know three ways to perform a server side include. Using include statements decreases file size, and it saves work if future editing is needed.

How To Make A Div Fill The Full Height of The Screen

September 20, 2009

This is a pretty simple procedure, but I thought that I would explain it anyway.

How to make a div 100 percent of the screen’s height:

  1. make the html and body have a height of 100%
  2. make the div have a height of 100%, and set its position to relative


html, body{height:100%;}
#mydiv{position:relative; height:100%;}

How To Make A Small Image A Full Screen Background

September 5, 2009

Is it possible to create a full screen background out of a 10×400 gradient image (or any other image)? Yes, it is quite simple using the css code below. After adding the following code to your css, just insert the image into your html and set its id equal to background to have a full screen background image.

body, html{
padding:0;
margin:0;
}
#background{
position:absolute;
top:0px;
left:0px;
width: 100%;
height:100%;
z-index:-1;
}

I searched Google countless times for a solution, and I could not find an answer. Finally, I gave up and settled for a different solution which I will discuss in a later post. However, while I was trying to solve another problem, I realized what was missing from one of my previous attempts. I only needed to adjust the padding and margin to 0 for the html and body elements.

Can you put a background image in a div element?

August 23, 2009

The answer is yes. You can put a background picture in a div using background-image in CSS. Below is an example piece of code.


div{
background-image:url("myimage.jpg");
height:100px;
}


It is important to remember the height line if you want the whole image to be displayed; otherwise, you will need a lot of text in the div in order for the whole image appear. If the div does not contain any text, and you do not include a line in the CSS specifying the height of the div; then, the background image will not appear at all.

Create Appearance of Transparent Background

August 18, 2009
example of transparent background

example of transparent background

First, a background image is needed.  Set this image as the body background using CSS.

For instance, the code below sets a body tag with the id homepg to have a background image called k3.gif.  Setting background-attachment to fixed causes the image to remain in the same position as you scroll. Background-repeat determines whether the image will appear one time or as a tessellation. Using background-position, one can determine the exact placement of the image.


body#homepg {
background-image:url('k3.gif');
background-repeat:no-repeat;
background-attachment:fixed;
background-position: 200px 50px;
}

Next, create the image to place on top. Use the same image that was used for the body background; however, watermark it using Microsoft Word or another program. Then, set the background of the div, p, dl or some other element which may be holding your content to the watermarked image using the same background-image CSS command that is used in the code above. If performed correctly, the result will appear like the example at the top of this post.