Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

Javascript.as

Name: Javascript Class [MX]
Author: senocular: www.senocular.com
Date: 1899-12-31T00:18:57.300
Documentation:
JAVASCRIPT CLASS: conduit for handling javascript in your
Flash movies. Pretty basic - included are some commonly used
Javascript functions which can be run as a method of a Javascript
instance or from the Javascript Class object directly.  All
scripts are added to a queue when called and run alltogether at
once in an enterFrame event in the following frame.  This
prevents possibly crashing which may occur otherwise.


JavascriptInstance = new Javascript("javascript");


Javascript instance Methods:
- setScript("javascript");
- load("url");
- run(["javascript"]);*
- statusBar("string");*
- windowSize(width,height);*
- windowPosition(x,y);*
- windowScroll(x,y);*
- openNewWindow("url","name","attributes");*
*methods are also available from the Javascript object directly (no instance)


JavascriptInstance.setScript("javascript");
- sets a "javascript" string to be the script of that Javascript object

JavascriptInstance.load("url");
- equivalent to XML.load - loads javascript from the url ("filename.js") into the Javascript
object.  When loading is complete, the onLoad event is called with a success true/false
argument to determine whether or not the loading was successful.

The following can be called
from the Javascript object
or an instance of the
Javascript object

run("javascript");
- runs the passed javascript string.  If no javascript is passed, and run is called from a
Javscript instance, the script of that instance is run.  This script is either set from the
new call or setScript.


statusBar("string"); - sets statusbar of browser to string passed
windowSize(width,height); - sets the window size of the browser to passed width and height
windowPosition(x,y); - sets the window position to the passed x and y
windowScroll(x,y); - sets the window scroll to the passed x and y
openNewWindow("url","name","attributes"); - opens a new browser window with the url and
name as specified in the passed attributes;

^ I tried to name these commands intuitively as opposed to their javascript counterparts.
This of course can be changed to your liking

Note:
- when escaping characters for javascript, realize that you will have to escape twice, once
for Flash and once for Javascript.  See the I'm in the statusBar example in the code area
- javascripts used should not exceed 250-300 characters fo

Example:
js = new Javascript("alert('hello');");
js.run();
js.setScript("alert('world');").run();
js.statusBar("hello, I'm your statusBar");
js.windowSize(400,400);
Javascript.windowPosition(0,30);
Javascript.windowScroll(0,200);
Javascript.openNewWindow("http://www.macromedia.com","MMhome","width=550,height=400");

externalJS = new Javascript();
externalJS.onLoad = function(){
	this.run();
}
externalJS.load("myjavascript.js");

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Javascript = function(script){
	if (arguments.length) this.setScript(script);
}
Javascript.broadcaster = Javascript.prototype.broadcaster = _level0.createEmptyMovieClip("JavascriptEnterFrame", 987654);
Javascript.broadcaster.onEnterFrame = function(){
	if (this.script.length) getURL("javascript:"+this.script+";void(0);");
	this.script="";
}
Javascript.prototype.load = LoadVars.prototype.load
Javascript.prototype.onData = function(js){
	if (js != undefined){
		this.script = js;
		this.onLoad(true)
	}else this.onLoad(false);
}
Javascript.prototype.setScript = function(str){
	this.script = str+";\n";
	return this;
}
Javascript.prototype.run = Javascript.run = function(str){
	if (!arguments.length) str = this.script;
	this.broadcaster.script += str+";\n";
}
Javascript.prototype.alert = Javascript.alert = function(str){
	this.run("alert('"+str+"')");
}
Javascript.prototype.statusBar = Javascript.statusBar = function(str){
	this.run("window.status='"+escape(str)+"'");
}
Javascript.prototype.windowSize = Javascript.windowSize = function(width,height){
	this.run("window.resizeTo("+width+","+height+")");
}
Javascript.prototype.windowPosition = Javascript.windowPosition = function(x,y){
	this.run("window.moveTo("+x+","+y+")");
}
Javascript.prototype.windowScroll = Javascript.windowScroll = function(x,y){
	this.run("window.scrollTo("+x+","+y+")");
}
Javascript.prototype.openNewWindow = Javascript.openNewWindow = function(url, name, attribs){
	this.run("window.open('"+url+"','"+name+"','"+attribs+"')");
}