PDA

Click to See Complete Forum and Search --> : Calling a Javascript Function from Flash



ryanmacflashkit
01-11-2007, 01:05 PM
Hi Guys,

I'm growing grey hair trying to figure this out, and all I can find on the net and resources is alert windows / fscommands / and as2 solutions which arent doing the trick except for fscommand which is kinda getting the job done but is redirecting the page at the same time. I have created this project using as1 and flash player 6 and thats why I haven't gone the as2 flash 8 nice and easy route for this solution.

Here's my problem/challenge.
I've got this javascript code in my html/php page which basically registers a sale from my flash app.

<script id="pap_x2s6df8d" src="sale.js" type="text/javascript"></script>
<script type="text/javascript"><!--
var TotalCost="1.00";
var OrderID="1";
var ProductID="1";
function testmovie_DoFSCommand(command, args) {
if (command == "call_alert") {
papSale();
}
}

--></script>
<SCRIPT LANGUAGE="VBScript">
<!--
// Catch the fscommand in ie with vbscript, and pass
// it on to JavaScript.

Sub testmovie_FSCommand(ByVal command, ByVal args)
call testmovie_DoFSCommand(command, args)
end sub

//-->
</SCRIPT>

On my flash frame I've got this...

fscommand("call_alert");

Which should just call the function and leave it at that. Good news is that it calls the function just, but why does my page then redirect???

Are there any alternatives using AS1? Or why is the page redirecting?

Thanks in Advance

rdoyle720
01-11-2007, 07:19 PM
Where is the page redirecting to?

ryanmacflashkit
01-12-2007, 05:46 AM
Hi RDoyle,

It redirects to itself. So it is the same URL but just a blank white screen. If I view source on that blank screen it shows a 1x1 pixel. Basically if the javascript code is executed from an HTML page it registers a sale on my affilliate system. I'm trying to get the call the same function and register a sale from flash. It registers a sale but why the blank screen.

Thanks in Advance

ryanmacflashkit
01-12-2007, 08:52 AM
As far as I can see the function papSale() includes another function that loads a flashcookie. So I think what is happening is I am calling the papSale() function from flash and it is replacing my current flash content with a flashcookie and thats what is causing the redirect/blank screen.

Here is the included sale.js file maybe it will make quick sense to you:

var SaleTracker = function(lid) {
this._lid = lid;

var trackingUrl = new String(document.getElementById('pap_x2s6df8d').src );
this._trackingUrl = trackingUrl.substr(0, Math.max(trackingUrl.lastIndexOf('\\'), trackingUrl.lastIndexOf('/'))+1);

this._trackingMethod = 5;

this._salerIndex = _salers.length;
_salers[this._salerIndex] = this;

this._cookies = new Array();

var cookie = new Object;
cookie.name = 'POSTAff2Cookie';
cookie.postname = 'fsc';
cookie.value = '';
this._cookies[0] = cookie;

var cookie = new Object;
cookie.name = 'POSTAff2TimeCookie';
cookie.postname = 'fstc';
cookie.value = '';
this._cookies[1] = cookie;

var cookie = new Object;
cookie.name = 'PAPR_0';
cookie.postname = 'forc';
cookie.value = '';
this._cookies[2] = cookie;
};

SaleTracker.prototype._getFlashVersion = function() {
var version = "", n=navigator;
if (n.plugins && n.plugins.length) {
for (var i=0; i < n.plugins.length;i++) {
if (n.plugins[i].name.indexOf('Shockwave Flash')!=-1) {
version = n.plugins[i].description.split('Shockwave Flash ')[1];
break;
}
}
} else if (window.ActiveXObject) {
for (var i=10; i>=4; i--) {
try {
var result = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash."+i+"');");
if (result) {
version = i + '.0';
break;
}
}
catch(e) {}
}
}
return version;
}

SaleTracker.prototype._isFlashActive = function() {
var version = this._getFlashVersion();
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all || (navigator.userAgent.indexOf('Opera') >= 0);
var ie4 = document.all;
if(!ns4 && !ns6 && ie4 && (this._saleType == 1)) {
return false;
}
return !(version == "" || version < 5);
}

SaleTracker.prototype._getNormalCookie = function(name) {
var nameequals = name + "=";
var beginpos = 0;
var beginpos2 = 0;
while (beginpos < document.cookie.length) {
beginpos2 = beginpos + name.length + 1;
if (document.cookie.substring(beginpos, beginpos2) == nameequals) {
var endpos = document.cookie.indexOf (";", beginpos2);
if (endpos == -1)
endpos = document.cookie.length;
return unescape(document.cookie.substring(beginpos2, endpos));
}
beginpos = document.cookie.indexOf(" ", beginpos) + 1;
if (beginpos == 0) break;
}

return null;
};

SaleTracker.prototype._getFlashParams = function() {
var params = "";
for(var i=0; i < this._cookies.length; i++) {
params += "&amp;n" + i + "=" + this._cookies[i].name;
}
return "?a=r" + params;
}

SaleTracker.prototype._getSaleParams = function() {
var params = "";
for(var i=0; i < this._cookies.length; i++) {
params += "&" + this._cookies[i].postname + "=" + this._cookies[i].value;
}
return params;
}

SaleTracker.prototype._loadFirstPartyCookies = function() {
for(var i=0; i < this._cookies.length; i++) {
var cookieValue = this._getNormalCookie(this._cookies[i].name);
if (cookieValue != null) {
this._cookies[i].value = cookieValue;
this._trackingMethod = 5;
}
}
}

SaleTracker.prototype._setCookie = function(name, value, type) {
for(var i=0; i < this._cookies.length; i++) {
if (this._cookies[i].name == name) {
this._cookies[i].value = value;
this._trackingMethod = type;
}
}
}

SaleTracker.prototype._writeImg = function() {
document.write('<img id="pap_0x25s6ds" src="" width="1" height="1">');
}

SaleTracker.prototype._loadFlashCookies = function() {
if(this._isFlashActive()) {
document.write("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" " +
"codebase=\"" + ((this._trackingUrl.substr(0, 5) == "https") ? "https" : "http") + "://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" " +
"width=\"1px\" height=\"1px\"> " +
"<param name=\"allowScriptAccess\" value=\"always\" />" +
"<param name=\"movie\" value=\"" + this._trackingUrl + "pap.swf"+ this._getFlashParams() +"\" /> " +
"<embed src=\"" + this._trackingUrl + "pap.swf"+ this._getFlashParams() +"\" width=\"1px\" height=\"1px\" allowScriptAccess=\"always\"/> " +
"</object>");
}
}

SaleTracker.prototype.trackNext = function() {
switch (this._saleType) {
case 0:
document.getElementById("pap_0x25s6ds").src = this._trackingUrl + "sale.php" +
"?lid=" + this._lid + "&trackingMethod=" + this._trackingMethod + "&TotalCost=" + this._totalCost + "&OrderID=" + this._orderID + "&ProductID=" + this._productID +
"&data1=" + this._data1 + "&data2=" + this._data2 + "&data3=" + this._data3 + this._getSaleParams();
break;
case 1:
var customInputs = document.getElementsByName("custom");
for (i=0;i<customInputs.length;i++) {
if (customInputs[i].id == "pap_dx8vc2s5") {
customInputs[i].value = this._cookies[0].value;
}
}
break;
}
}

SaleTracker.prototype.processFlashCookies = function(cookies) {
var flashCookies = cookies.split('_,_');
for(var i=0; i< flashCookies.length; i++) {
var splitIndex = flashCookies[i].indexOf('=');
if (splitIndex < 0) continue;
if ((flashCookies[i].substr(splitIndex+1) == null) || (flashCookies[i].substr(splitIndex+1) == '')) continue;
this._setCookie(flashCookies[i].substr(0, splitIndex), flashCookies[i].substr(splitIndex+1), 6);
}
}

SaleTracker.prototype.paypalPreSale = function() {
this._loadFirstPartyCookies();
if (this._cookies[0].value == '' || this._cookies[0].value == null) {
document.write('<script src="' + this._trackingUrl + 'presale.php?lid=' + this._lid + '&salerIndex=' + this._salerIndex + '"></script>');
} else {
this.sale();
}
}

SaleTracker.prototype.sale = function() {
this._writeImg();
this._loadFirstPartyCookies();
if (this._isFlashActive()) {
this._loadFlashCookies();
setTimeout('trackNext('+this._salerIndex+')', 1000);
} else {
this.trackNext();
}
}

var _saler;
try {
var pap_tmp = _salers.length
} catch (err) {
var _salers = new Array();
}

function trackNext(salerIndex) {
_salers[salerIndex].trackNext();
}

function rpap(cookies) {
for (i=0;i<_salers.length;i++) {
_salers[i].processFlashCookies(cookies);
}
}

function set3rdPartyCookie(salerIndex, name, value) {
_salers[salerIndex]._setCookie(name, value, 1);
}

function finishPaypalSale(salerIndex) {
_salers[salerIndex].sale();
}

function papSale() {
try { _saler = new SaleTracker(_lid); } catch (err) { _saler = new SaleTracker(''); }
try { _saler._totalCost = TotalCost; } catch (err) { _saler._totalCost = 0; }
try { _saler._orderID = OrderID; } catch (err) { _saler._orderID = ''; }
try { _saler._productID = ProductID; } catch (err) { _saler._productID = ''; }
try { _saler._data1 = Data1; } catch (err) { _saler._data1 = ''; }
try { _saler._data2 = Data2; } catch (err) { _saler._data2 = ''; }
try { _saler._data3 = Data3; } catch (err) { _saler._data3 = ''; }
try { _saler._secureConnection = _sc; } catch (err) { _saler._secureConnection = false; }
_saler._saleType = 0;
_saler.sale();
}

function paypalSale() {
try {
_saler = new SaleTracker(_lid);
} catch (err) {
_saler = new SaleTracker('');
}
try { _saler._secureConnection = _sc; } catch (err) { _saler._secureConnection = false; }
_saler._totalCost = '';
_saler._orderID = '';
_saler._productID = '';
_saler._data1 = '';
_saler._data2 = '';
_saler._data3 = '';
_saler._saleType = 1;
_saler.paypalPreSale();
}

Thanks in Advance

ryanmacflashkit
01-12-2007, 09:53 AM
Looks like the function is calling a flashcookie call and this is replacing the content with some sort of tracking pixel.

ryanmacflashkit
01-12-2007, 02:34 PM
You know what, now I understand why they added the external face call in AS2. Flash Javascript in AS1 is not hip and happening. I have resorted to good old loadVars to the core php file instead of *****footing around this js issue. Case Closed