Jan 25, 2008

Flash: How to read variables from the URL


How to transfer variables from url string to Flash movie.


Seems that it will be the shortest tutorial in the world :) I always thought that it is obvious and everybody know it, but there were too many questions about it.

So lets say that you have GET style url string like

http://somethere.com/somename.html?firstvar=something&anothervar=12

And if you want to have variables firstvar and anothervar inside flash movie on that page, the
only thing that you have to add in html code is (red color)

<HTML>
<HEAD>
<TITLE>url</TITLE>
</HEAD>
<BODY bgcolor="#669966" marginwidth="0" marginheight="0"
border="0" topmargin="0" leftmargin="0">

<script language="JavaScript">
document.write ('
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
+'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" '
+'WIDTH=100% HEIGHT=100%> <PARAM NAME=movie
VALUE="url.swf'+document.location.search+'">'
+'<PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#669966>'
+'
<EMBED src="url.swf'
+document.location.search+
'" '
+'quality=high bgcolor=#669966 WIDTH=100% HEIGHT=100% '
+'TYPE="application/x-shockwave-flash"></EMBED></OBJECT>
')
</script>
</BODY>
</HTML>

And one last thing :) It will not work locally from disk or as file:///...
To test it you must call it from local web server or upload it in the net.

Besides
You may use server scripts to do same things.

ASP:
http://somethere.com/somename.asp?firstvar=something&anothervar=12

<HTML>
<HEAD>
<TITLE>url</TITLE>
</HEAD>
<BODY bgcolor="#669966" marginwidth="0" marginheight="0"
border="0" topmargin="0" leftmargin="0">

<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"
WIDTH=100% HEIGHT=100%> <PARAM NAME=movie
VALUE="url.swf?<%=Request.QueryString()%>">
<PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#669966>
<EMBED src="url.swf?<%=Request.QueryString()%>"
quality=high bgcolor=#669966 WIDTH=100% HEIGHT=100%
TYPE="application/x-shockwave-flash"></EMBED></OBJECT>

</BODY>
</HTML>


PHP:
http://somethere.com/somename.php?firstvar=something&anothervar=12

<HTML>
<HEAD>
<TITLE>url</TITLE>
</HEAD>
<BODY bgcolor="#669966" marginwidth="0" marginheight="0"
border="0" topmargin="0" leftmargin="0">

<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"
WIDTH=100% HEIGHT=100%> <PARAM NAME=movie
VALUE="url.swf?<? echo $QUERY_STRING;?>">
<PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#669966>
<EMBED src="url.swf?<? echo $QUERY_STRING;?>"
quality=high bgcolor=#669966 WIDTH=100% HEIGHT=100%
TYPE="application/x-shockwave-flash"></EMBED></OBJECT>

</BODY>
</HTML>

But
It is not the best way for one easy reason - after changing value of some variable in url string your swf movie will not be loaded from browser cache - it will reload from the net. It is not good if your movie large ! The only two ways to fight against it is using loading variables from server scripts and using JS SetVariable (that last will not work in mac\IE, Opera and NN6)

Hope that you will be able to write easy server or java scripts for it yourself :) I have a lot of examples about server scripts and JS setvariables on main page :)

It is all :) Try to test it here.

No comments: