Thursday, 31 July 2008

Enable an HTML Form to Submit to Multiple Locations Using Javascript

This is a simple solution to allow a single html form to submit to multiple locations.

<form name="myform" method="post" action="noscriptPage.php">
...
<input type="submit" name="submit" value="Update"
onclick="document.myform.action='updatePage.php';"/>
<input type="submit" name="submit" value="Submit"
onclick="document.myform.action='submitPage.php';"/>
</form>

Add the multiple submit buttons to the form, each with a Javascript onclick clause to change the forms action property, to the desired action page.

Easy.

Optionally you can also cater for users with Javascript disabled, by adding a default action to the form, to either go to a message page, forward page that can determine the button pressed and forward.

Wednesday, 30 July 2008

Oracle Forms: Jar Icon Images

Oracle web forms uses image files for icons, by default forms will get each image individually, wasting time and bandwidth.

If you add the images to a jar file they are compressed and are loaded at startup, so each form wouldn't need to load the images each time. It also seems like a neater solution.

This code is a windows command script for creating the icon jar file.
The three parameters need to be set for each system.
  • oh = Oracle home
  • id = Icon directory
  • jf = Jar filename
@ECHO OFF

TITLE Jar Icons

SET oh="C:\Oracle\DS10"
SET id="C:\MyApp\icons"
SET jf="C:\MyApp\icons.jar"

CD /D %id%

%oh%\jdk\bin\jar -cvf %jf% *.*

TITLE Jar Icons Complete

PAUSE

You then need to place the jar file on to your web server, so the web server can access them via a URL.
If you placed the jar file in the forms/java directory you wouldn't need a path, however mixing application and forms files isn't best practice.
The URL path to the jar file needs to be added to the archive parameter in forms/server/formsweb.cfg file, so for example:

archive=frmall.jar,myIconPath/icons.jar

or in the archive_jini parameter, if you use JInitiator

archive_jini=frmall_jinit.jar,myIconPath/icons.jar

You also need to set the imagebase parameter

imagebase=codebase

You can then reference the icons by name inside the form and they should then be displayed.

Quicker, Faster, Neater Forms Icons.. Done.

Tuesday, 29 July 2008

Oracle Forms: Web.Show_Document without Toolbars

When using web deployed Oracle Forms, it was really annoying that we just wanted to display a report or popup page in the broswer without the toolbars and buttons. They weren't necessary and they cluttered up the page. The web.show_document command didn't have an option to remove them so we came up with a little routine to get around the problem and make things a little nicer.

First we added a hidden iframe, to the page forms was running on and gave it the name "hiddenframe".

<iframe name="hiddenframe" style="display:none"></iframe>

We then created a new html page, "newWindow.html".

<html>
  <body>
    <h1>Open Page in a New Window</h1>
    <script type="text/javascript">
      var thisUrl = document.location.toString();
      var newPage = thisUrl.substring(thisUrl.indexOf("?",0)+1);
      var openWin = window.open(newPage,"_blank","resizable=yes,scrollbars=yes");
      openWin.focus();
    </script>
  </body>
</html>

This page is always called into "hiddenframe" with the report page we want to display passed as a parameter. Javascript in the "newWindow.html" page then opens a new window without toolbars and buttons and calls the desired page.

So if I wanted to open a page called myReport.html in a new window without toolbars and buttons I can call web.show_document as:

WEB.SHOW_DOCUMENT('http://myserver/newWindow.html?http://myserver/pls/myReport.html', 'hiddenframe');

The user needs popup blockers turned off for this server but this shouldn't be an issue for business applications of this type.

This is not restricted to Oracle Forms, it's so simple it can be used for any web application.
You can use this test page to test the newWindow.html page. Enter the url to open in the input box.

<html>
  <body>
    <h1>New Window Test</h1>
    URL: <input id="page" type="text" value="http://www.google.com/"/>
    <input type="button" value="Open" onclick="window.open('newWindow.html?'+document.getElementById('page').value,'hiddenframe')"/>
    <iframe name="hiddenframe" style="display:none"></iframe>
  </body>
</html>

Done...

Monday, 28 July 2008

Get URL Parameters Using Javascript

Most URL parameters are read and processed by server side code, however you can access them easily by using Javascript. Add the function from this example to your page and call it, passing it the parameter name you want to read.

<html>
<head>
<script>
function getParam(name)
{
var start=location.search.indexOf("?"+name+"=");
if (start<0)
start=location.search.indexOf("&"+name+"=");
if (start<0)
return '';
start += name.length+2;
var end=location.search.indexOf("&",start)-1;
if (end<0)
end=location.search.length;
var result=location.search.substring(start,end);
var result='';
for(var i=start;i<=end;i++)
{
var c=location.search.charAt(i);
result=result+(c=='+'?' ':c);
}
return unescape(result);
}
</script>
</head>
<body>
<script>
document.write(getParam("test1")+"<br>");
document.write(getParam("test2")+"<br>");
</script>
</body>
</html>

So to test the code call it up in your browser and add the following to the end of the URL in the address bar.

?test1=Hello&test2=World

The test page will then display the parameter values

....Done.

Thursday, 24 July 2008

Java Session Cookies v HttpSession

Having written some Java Servlet code, which stored a string in a session cookie, I then looked into how HttpSession would compare and found I can get the same results with much simpler code.

My original session cookie code:

private String mySessionValue;

private String getMySessionValue(HttpServletRequest request) {
if(mySessionValue == null) {
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(int i=0; i < cookies.length; i++) {
Cookie c = cookies[i];
if (c.getName().equals("mySessionValue")) {
mySessionValue = c.getValue();
break;
}
}
}
}
return mySessionValue;
}

private void setMySessionValue(HttpServletRequest request, HttpServletResponse response, String newCookieValue) {
mySessionValue = newCookieValue;
Cookie cookie = new Cookie("mySessionValue", newCookieValue);
response.addCookie(cookie);
}

I had several issues with the cookie version,
  • I had to loop through the cookies to find my cookie
  • As the cookie value could be referenced after it was set, I had to store it locally too, as the actual cookie would not be updated until the responce is sent.
With HttpSession the code is much much less and no need to store the value locally:

private String getMySessionValue(HttpServletRequest request) {
HttpSession session = request.getSession(true);
return (String)session.getAttribute("mySessionValue");
}

private void setMySessionValue(HttpServletRequest request, String newSessionValue) {
HttpSession session = request.getSession(true);
session.setAttribute("mySessionValue", newSessionValue);
}

As well as much less code one main advantage to HttpSession is that it handles Objects not just Strings, so arrays can be stored much easier.

HttpSession wins I think.

Wednesday, 23 July 2008

Daily Notes

Something I have found very useful over the last year is a notepad script I wrote, that creates me a new text file for each day I use it. Whether it be for little code snippets, notes taken whilst on the phone, interesting websites or whatever, the "Day Notes" shortcut on my taskbar is used alot.

I know there are many free note taking programs available but I like to keep things simple and they don't get much simplier than text files. So:

1) Create a directory where you want to keep your day note files. EG: C:\DayNotes

2) Create a text file called "DayNotes.cmd", edit it with notepad and paste in the following code and save.

@ECHO OFF

SET DAYNOTEHOME=C:\DayNotes

REM ------------------------
REM -- Set today variable

FOR /f "tokens=1,2,3 delims=/" %%A in ('echo %DATE%') DO SET TODAY=%%C-%%B-%%A.txt

IF NOT EXIST "%DAYNOTEHOME%\%TODAY%" (
ECHO %DATE% >> "%DAYNOTEHOME%\%TODAY%"
ECHO ========== >> "%DAYNOTEHOME%\%TODAY%"
)

START NOTEPAD.EXE "%DAYNOTEHOME%\%TODAY%"

3) If you want, create a shortcut and place it on your taskbar. Set the Run property to Minimized, so you don't get a command window flash up.

When you run the script it creates a text file in your day notes directory, EG: 2008-07-23.txt, if it doesn't already exist, so you can easily refer back to notes made that day.

If I can't remember when I had a particular telephone call or wrote some notes, out comes the windows search, which finds it quite quickly.


Daily Notes Script...Done.

Tuesday, 22 July 2008

Wrap Oracle PLSQL Routine

This Windows command script uses Oracle's wrap utility to wrap the contents of an entire directory. The wrapped files are placed in a sub directory is created called "Wrapped", which is created if it doesn't exist.

Copy the following code into a file called "WrapDirectory.cmd".

@ECHO OFF

TITLE Wrap Directory

SET /p src="Enter Source Directory...: "
SET ora=E:\Oracle\DB10

IF NOT EXIST %src%\Wrapped MKDIR %src%\Wrapped

TITLE Wrapping Directory

FOR %%F IN (%src%\*.sql) DO CALL %ora%\Bin\WRAP INAME=%%F ONAME=%%~dpF\Wrapped\%%~nF.plb >> %%~dpF\Wrapped\wrap.log

TITLE Wrapped Directory

PAUSE

The Oracle home directory needs to be configured to your own home, before it can be run. It can also be easily modified to wrap files other than ".sql" which it currently does.

This has been been written and tested against Oracle 10g.

PLSQL Files... Wrapped.

Monday, 21 July 2008

Consider Printing When Designing Web Pages

When designing a web site you should consider the media with which it is going to be viewed. Currently the most common is on the screen through the web browser but what is often not considered is, what does this look like when printed?

Some websites have links to printable pages but this is only necessary if you want to accommodate browsers from the dark ages. CSS2 allows 9 different media types: aural, braille, embossed, handheld, print, projection, screen, tty and tv, so the same web paged can be viewed in a style suitable for the media without the need for separate pages.

To get the best screen and print layouts for the same page, you need to add separate style sheets for screen and printing:

<link rel="stylesheet" type="text/css" href="screen.css" media="screen">
<link rel="stylesheet" type="text/css" href="print.css" media="print">

Or if you want to import:

<style type="text/css" media="print">
@import "print.css";
</style>

Or, you can specify a style sheet for multiple media:

<style type="text/css" media="screen, print">
  @import "print.css";
</style>

You can also specify within the style sheet by wrapping a style section:
<style type="text/css">
@media print {
#menu { display: none; }
}
</style>

As the screen is interactive and a print out is not, you need to consider which parts of the page should be included when printed. Sections such as menus, adverts and links are of no use on a printed page, as the user can not click on them, so you can hide them by using the display:none css property.

Also consider that the page maybe printed in black, so change highlights using colours to bold, italics or underlined.

The font may also need to be changed. Serif fonts, such as Times Roman or Garamond, are generally easier to read when printed, where as sans serif, such as Arial or Helvetica, would be used on the screen as they are simpler. The text size should be in points as pixels can sometimes give strange results.

Keep images, especially backgrounds to a minimum, the user would not thank you for using up all of their ink on a pretty background.

Friday, 18 July 2008

Backup USB Memory Stick Files

This is a little short Windows command script to enable you to backup your USB memory stick to your PC's hard drive. (Although I say USB Memory stick, it can backup any directory). You'll need to create a directory to place the script in, the backups are then placed in dated sub directories, so you can keep them as archives if you wish.

1) Decide where you want to keep your backups and create a directory.

2) Create a file called "USBbackup.cmd"

3) Edit the file using Notepad, copying the following text into the file.

@echo off
title USB BackUp
cd /d %~dp0
set /p BACKUPDIR="Enter drive letter to be backed up..: "

REM ====
REM Check if backup source is drive or directory

if exist "%BACKUPDIR%:\." ( set BACKUPDIR=%BACKUPDIR%:
) else ( if not exist %BACKUPDIR%\. goto :error )

REM ====
REM Create sub directory based on date.

for /f "tokens=1,2,3 delims=/" %%A in ('echo %DATE%') do set TARGET=Backup-%%C%%B%%A
for /f "tokens=1,2,3 delims=:." %%A in ('echo %TIME%') do set TARGET=%TARGET%-%%A%%B%%C

REM ====
REM Check source and target

echo Backing up from drive "%BACKUPDIR%" to "%TARGET%"
pause

REM ====
REM Create target directory and backup files

title Backing up files
if not exist ".\%TARGET%\." mkdir .\%TARGET%
XCOPY /E "%BACKUPDIR%\*.*" ".\%TARGET%\"

REM ====
REM Finalise

title Usb Backup Complete
goto :end
:error
title Usb Backup Error
echo Unable to locate drive "%BACKUPDIR%"
:end
pause

4) Save the file and run it by double clicking on it.

5) When prompt, enter the drive letter or directory to be backed up.


USB Stick backup... Done.

Thursday, 17 July 2008

Some Amusing Javascript

Found this little Javascript feature in a forum.
Call up any web page and then paste the following code into the address bar.

javascript: document.body.contentEditable = 'true'; document.designMode = 'on'; void 0

You can then have a bit of fun editing the page.
Weird!

Wednesday, 16 July 2008

Create a Windows Show Desktop Icon

After installing Windows 2003 Server, I found that it doesn't have a show desktop shortcut on the taskbar, which was annoying as I found this useful on XP. So I spent a few minutes to create one with these few steps.

1) Create a file "Show Desktop.scf" in the windows folder.

2) Edit the new file in notepad, adding the following lines:

[Shell]
Command=2
IconFile=explorer.exe,3
[Taskbar]
Command=ToggleDesktop

3) Create a shortcut and add it to the taskbar.

Show Desktop icon, created.

Tuesday, 15 July 2008

Check If Your Hosted Website Has Access To PHP

When I built my hosted website I didn't intend to use PHP, so later on when I wanted to try out some PHP, I wasn't sure if I had access to use it and if I did which version of PHP it was.
I had to do a lot of searching to find out how to check if I had PHP or not, but when I found a solution I realised that it was probably because it is very simple. But these things are only simple if you know, so..

1) Create a file on your website called phpinfo.php (Can be called anything but should end with .php)
2) The file should contain one line:

<?php phpinfo(); ?>

3) Call the file through your web browser. If you have PHP it should come back with the version details.

Done...

Monday, 14 July 2008

Javascript Random Numbers

OK then, to generate a random number in javascript you need to use

Math.random()

which produces a number between 0 and 1.

I began by multipling the random number by the maxmium value to give a random number in a range. So for a number between 0 and 4,

Math.round(Math.random()*4)

However this will not give you a true result for 0 or 4, as the rounding for 0 and 4 will have half the possible candidate random numbers.
So another possibility is to use floor instead of round and use the range instead of the max value.

Math.floor(Math.random()*5)

This is floored in principle if the random number returns 1, which is a 1 in a gazillion billion chance. (Haven't actually proved 1 is a possible return value, but assuming it is) So to be clinically correct we can mod the result to always give a value answer.

Math.floor(Math.random()*5)%5

And an example to prove the spread of random numbers:

<html><body>
<script>
var a = new Array(0,0,0,0,0);
for(i=0;i<1000;i++)
{
a[Math.floor(Math.random()*5)%5] += 1;
}
document.write(a+"<br/>");
document.write("Total: "+(a[0]+a[1]+a[2]+a[3]+a[4]));
</script>
</body></html>

Javascript Random Numbers ... Done.

Friday, 11 July 2008

Welcome

This blog is my jotter for making notes for niffty software development routines I create or find, things of interest I discover on the web or general stuff that may I may want to remember for later, or just anything I fancy writing :-)

If you're not me and are here searching for something, I hope this has been useful or somehow interesting.