Chris Worfolk's Blog


Opening pop-ups in Flash

December 30th, 2004 | Programming, Tech

You can call JavaScript functions from your flash movie. This tutorial will show you how to call a pop up window opener function from your movie when a user clicks a button. You can create the html page in any text editor.

1. Create a simple button in Flash. You could also just drag one out of the shared library. You then need to open up the actions menu and insert the following code:

on (release) {
getURL("JavaScript:popup();");
}

This code will call the JavaScript when a user clicks on the button. Once this is done goto File > Publish and publish your movie. Make sure the publish settings are set to publish the file with a html document.

2. Now you need the code for the JavaScript function.

<script language="JavaScript">
function popup() {
window.open('http://www.hardwaretutorials.com/','','toolbar=no,location=no,directories=no,status=no,menubar=no,
scrollbars=yes,resizable=yes,width=400,height=400,left=0,top=0');
}
</script>

This code can be modified. The first section is the page URI. The second section is your options such as do you want to make it resizable and have scrollbars. The first section has width, height and location from left and top in. Make sure the “window.open” line and the line below are on the same line.

3. Now open your text editor and find the html file generated when you published your movie. Open it and insert the JavaScript function just below the tag. Now save the page.

4. Open the file in your browser. Clicking the button should now open up the pop up window.

Making your Flash movie transparent

December 30th, 2004 | Programming, Tech

Making your movies transparent allows you to see the background on bits where there is nothing in the movie. However this is actually nothing to do with the settings in Flash when publishing movies. The trick is in the html file that the movie is added into.

Transparency in movies will only work in Internet Explorer 4+ and won’t work in Netscape.

To make your movies transparent, simply paste this code in between the HTML Object tags.

<param NAME="wmode" Value="transparent"

And so, the embedding code for the SWF file for the second example (transparent) is as follows:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ID="himalayas" WIDTH="250" HEIGHT="100">
<param NAME="movie" VALUE="himalayas.swf">
<param NAME="quality" VALUE="high">
<param NAME="wmode" Value="Transparent">
<embed src="himalayas.swf" quality="high" bgcolor="#FFFFFF"
WIDTH="250" HEIGHT="100" TYPE="application/x-shockwave-flash">
</object>

Note: the above source file doesn’t include some key information like the Base Code URL due to the text width limitations.

Limitations

The transparency (as well as any other cool new technology) doesn’t work in Netscape at all. Use Internet Explorer to be able to view transparent Flash Content.

For the transparency effect to work as desired, don’t add a filler background element to your movies. Many developers add a large, white rectangular box, etc. to simulate white color. Make sure the drawing area behind the main movie is clear of any unwanted colours, etc (it will show up in the final animation).

Using timers in Visual Basic

December 30th, 2004 | Programming, Tech

Timers are those little button things that appear on the wizard for the web browser from in Visual Basic 6. They control time. The first thing to note about them is that they are invisible at run time so you don’t have to worry about where you put them because the user won’t see a thing.

Timers, wait for a specific length of time and then execute the script they have. One of the first options they have is whether to start enabled or disabled. If it’s enabled it will begin timing and execute the script. If it is disabled then it won’t do anything.

Being able to disable it allows you to set it so it only does something once. For instance if you wanted it to wait 10 seconds and then say “hello,” but only do this once you could use this:

Create a timer object called “timer1.” This should happen automatically as timer1 is the default name. Set your time to enabled and set the time period to 10000. The timer is set to one thousandth of a second so 10,000 will make it time for ten seconds.

Next double click on the timer object to bring up your code box. And add in the following:

MsgBox ("Hello")
Timer1.Enable = False

When the timer reaches its time limit it runs the script. The first line just tells it to say “hello” in a message box. The second line disables itself by telling the object “timer1” to set its property, enable to false so that it is disabled.

A timer is also handy in things such as web browsers. Every tenth of a second you could set the timer to check whether a page has loaded and if so change the title of the form to the web browser’s page title.

If WebBrowser1.PageTitle <> Form1.Caption Then
Form1.Caption = WebBrowser1.PageTitle
End If

In this script you want to keep the timer permanently enabled so it will be constantly checking to see if the web browser’s page title and the text in the title bar of the forms are different and if so, update the forms title.

Adding data to the registry in Visual Basic

December 30th, 2004 | Programming, Tech

This sample shows a bottom which when clicked changes the registry. The first three bits of information are the categorises in the registry while the final one is the value.

Private Sub button1_Click()
SaveSetting "My App", "Options", "Clicked", "Yes"
End Sub

You can also make the information dynamic. For instance if you wanted to save a users name you could have a text box called Name1. When the clicked a button it would then save the name they entered into the registry.

Private Sub button1_Click()
SaveSetting "My App", "Users", "Username", Name1.Text
End Sub

Reading from the Registry in Visual Basic

December 30th, 2004 | Programming, Tech

In a previous article I showed you how to save settings in the registry. Now I’m going to show you how to get them back out again.

The basic syntax is as follows.

GetSetting("MyApp", "Category", "InfoName", "DefaultValue")

So lets look at the article on saving information in the registry. I used this example to save a username.

SaveSetting "My App", "Users", "Username", Name1.Text

Now, we’re going to pull this information out and and display the username in the title of the application. This code goes in the Load sub in your application. The name of the form we are working on is irrelevant because I used the term “me” which refers to the current form the code is on.

Me.Caption = "Weclcome " + GetSetting("MyApp", "Users", "Username", "To MyApp")

If no value is found in the registery, the default value is used so if no username was present, the applications title would be.

Welcome To MyApp

If the username in the register was “Jimbo” the applications title would be.

Welcome Jimbo

When using GetSettings you can’t use it on its own. You must use as as part of an equasion such as the example below.

A = GetSettings

You can also do the following.

If GetSettings = a Then do b

It simply supplies one piece of information from the Registry just like a variable.

Trapping errors in Visual Basic

December 30th, 2004 | Programming, Tech

If you keep coming up with problems in your applications and you don’t want error messages to be popping up when the user causes an error then all you need to do is add an error handler. In fact, you should be doing this anyway, just in case errors come up.

On Error

There are two options from here. You can either get it to ignore the error or do something else that sorts the error out. If you just want the error to be ignored use this.

On Error Resume Next

This will simply execute the next line of code after one with the error on. This could stop your application doing something important but lets face it, however there are times when you legitimately want to stop an error and ignore it. Usually it is just used then there is a problem such as when you are using the web browser control and a user clicks back when there is no page to go back to. This would normally bring up an error box but by adding in the code it stops this from happening.

Your other alternative is to add in a GOTO command to send the code to do something else if it finds an error. Take a look at this example.

On Error Goto 10
WebBrowser1.GoBack
Exit Sub
10 ' error bit
MsgBox ("Error!")
' some code to sort it out
End Sub

Turning a form button into a link using JavaScript

December 30th, 2004 | Programming, Tech

Bored of ordinary links? Then try using buttons. This can work well if there are a series of them lined up in a navigation bar especially if you using image backgrounds or CSS styles to make them look good.

First of all, there is the script that goes into the head of your page.

<script language="JavaScript">
<!--
function goToURL() { window.location = "http://www.hardwaretutorials.com/"; }
-->
</script>

Amazingly simple. Your normal JavaScript tags with a link function. Now for the button which goes in your pages body tag:

<input type=button value="New JavaScripts" onClick="goToURL()" />

Of course, you should never use buttons, or JavaScript links, because they create usability problems. Also, this is also invalid HTML. But it will work in almost all browsers.

Suppressing JavaScript errors

December 30th, 2004 | Programming, Tech

What is the most annoying thing you cant think of? Well ok, JavaScript error aren’t that annoying but nobody likes them. What we need is a simple script that stops all the JavaScript erros from appearing. This is a pretty simple script which is short so lets dive straight into the code.

<script>
function stoperror(){
return true
}
window.onerror=stoperror
</script>

Just stick the script into the head tag. Although the above alert method is invalid (no closing quotations), no errors will be generated. This script will suppress all potential errors in a page, so be sure to turn it off while test running a script.

Of course, a much better solution, is to actually write JavaScript that works.

A tale of two web servers

December 30th, 2004 | Life

mworld.us is back baby! After the Audio Senate revivial on December 6th (turns out Nerd Federations track back system isn’t what I thought it was 😉 I didn’t think a revival could get much sweeter. But now M World is back it is a great feeling. Even if it did take so many hours.

To begin the story, I renewed by web hosting back at the end of October. My web host, onedollarhost.net, didn’t seem to notice though and deleted everything. Files, databases, apparently they don’t even keep backups. Hmm, so as you can image I wasn’t happy. M World has like a hundred sub-sites so I can’t just upload them all again.

Anyway I emailed them several times asking if they had backups and if so use them to restore the files from that. The response I got over and over was:

“Files were deleted. You will need to re-upload the files.”

Another thing I wanted from that host was stats as I had none. It is a shame I didn’t as it was quite a high traffic site with over 6,000 pages in Google so I would be interested to see what the traffic was like. That is no longer a problem though, as I took the decision to move over to my new Windows host.

I have been with them ages although I didn’t want to move M World because of the data loss (and there has been a lot of data loss). Still it didn’t seem to matter now, so I moved it over and so from now on I will be able to track everything from Awstats. There is some good in every situation I guess.

I have also spend hours last night and most of today putting together all of the sub-sites. I found all but one although there may be a load not listed. Still, I have put all the others online and with no content, hopefully M World will eventually regain it’s former glory.

So although it has not been a good experiance – two months of missing files, most of it is back online now. There are still a few bits I keep remembering but overall, I think M World can now move forward, safe on a new server.

Find your IP address in Windows

December 30th, 2004 | Life, Tech

To find out your ip address:

Select start and click run.

Type “command” and click ok.

Once the command promt appears, type ipconfig and press return. This will then bring up a list of different ip address’s. You will usually be looking for the port you connect to the internet through.