Tuesday, 10 April 2012

How to use javascript to change an element's attributes

document.getElementById('id').setAttribute("style","position:absolute;left:10px");


This method is useful if you have a lot of properties to add, but it can overwrite the other style attributes that you may have already set.


An alternative method is:

document.getElementById('id').style.width = "500px";

Sunday, 8 April 2012

Javascript: Rewrite element content

How to change the content of an element...?

In the head tag:

<script type="text/javascript">
function giveWidth(e) {
 document.getElementById('wid').innerHTML = window.innerWidth;
}
</script>

In the body tag:

<p>Window width <a href="" onclick="giveWidth(event);return false">is</a> <span id="wid">500</span> pixels.<p>

The return false makes sure that the browser doesn't try to open a new window when you click on the link.

Tuesday, 27 March 2012

How to Make Your swf Resize to Fit The Browser Window Automatically

If you want your swf to be as big as it can be in the browser window, and to resize automatically fit the user resizes the browser window:

Change the width and height values to 100%, and the scale to "default".

e.g.

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="oxphos" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="oxphos.swf" />
<param name="loop" value="false" />
<param name="menu" value="false" />
<param name="quality" value="high" /> 
<param name="scale" value="default" />
<param name="bgcolor" value="#ffffff" />
<embed src="oxphos.swf" loop="false" menu="false" quality="high" scale="default" bgcolor="#ffffff" width="100%" height="100%" name="oxphos" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
 </object>

Obviously, if you have the script included, then you will need to change the values there too.

By the same token, you could set the swf to fill up most, but not all of the screen, by setting width and height to 80%, for example.

Thursday, 22 March 2012

How to Create a MySQL Database if it Doesn't Exist - Very Easily

This is so easy I almost don't believe it, except I've tested it and it works.

$create = mysql_query("CREATE DATABASE IF NOT EXISTS databasename");

Crazy easy, no?

Sunday, 11 March 2012

Mariam's clock

Note to teacher: Mariam did this herself, she is quite familiar with Flash by now, though she did need some help. When she needed help, I told how to do things, so she did them herself. From this experience, one of the things I could see her learning was that the numbers have to be positioned correctly, or a 'quarter' isn't really a quarter. So she repositioned some numbers in the last page.



Monday, 7 November 2011

One of these things is not like the others...

When does 1 != 1???

I was trying to compare two values, if they were the same, then their associated movie clips had been correctly linked to each other.

Trouble was, according to flash, one movie clip's 1 was not the same as the other movie clip's 1, even after I had used Number() to convert them both to numbers.

According to Flash, Number(1) = NaN!!!

Explanation:

One of the movie clips had been given a number from a for loop, the other had been given a number from an array. (Still doesn't really make sense to me, I'm sure I've done this before and never had this problem, but anyway...)

Solution:
Convert the value to a string, then convert that to a number.

e.g. Number(String(mc.val))

Weird...

Monday, 3 October 2011

How to Shuffle an Array in One Line

I often have to get a randomised version of an array, and I used to do it like this:

Create an empty array for the shuffled version
(Within a for loop) {
Get a random index using Math.floor(Math.random() * array.length)
Put the value from the unordered array into the ordered array, using the random index
Remove the value from the unordered array, again using the random index.
}

But now:

(Within a for loop) {
array.push(array.splice(Math.floor(Math.random() * array.length),1));
}

One line, no need for an empty array to be created.