Trigger an actionscript function (in a flash object) from javascript - javascript

I have a flash (AS2.0) application with a function that i need to trigger from a html form link
The flash function only runs a gotoAndPlay('label name');
So my HTML is
<a href="" id="flashTrigger" />
and my flash function is
function myFunction(){
gotoAndPlay("myLabel");
}
Anyone know either how I can fire the flash function from the html link tag, OR run a "gotoAndPlay" from a Javascript function
Iv looked around and only seem to find how to fire a javascript function from flash
Here is the code I have so far - Im prob doing something stupid
Flash:
ExternalInterface.addCallback( "myExternalMethod", this, myFunction );
function myFunction(){
gotoAndPlay("fade");
}
Javascript
function executeFlash()
{
getObjectById("myFlashID").myExternalMethod();
}
function getObjectById(objectIdStr) {
var r = null;
var o = document.getElementById(objectIdStr);
if (o && o.nodeName == "OBJECT") {
if (typeof o.SetVariable != undefined) {
r = o;
}
else {
var n = o.getElementsByTagName(OBJECT)[0];
if (n) {
r = n;
}
}
}
return r;
}
$(function() {
$('#WhatDoesmean').click(function(){
executeFlash();
});
});
I have set myFlashID to the id of:
initial 'Object tag'
and IE only 'embed tag'
EDIT:
At the moment I am targeting the flash object fine, it's the external (flash side) function which is not working
- error message, myExternalMethod is not a function

You can use the external interface for this:
There is a working example at this location:
Flash to JS:
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001655.html
JS to Flash:
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001653.html
Hope this helps
Cheers

I used this a few years ago to capture the flash object:
var flashObj = getFlash("Flash_Name");
function getFlash(movieName)
{
if (window.document[movieName])
{
return (window.document[movieName]);
}
if (navigator.appName.indexOf("Microsoft Internet")==-1)
{
if (document.embeds && document.embeds[movieName])
{
return (document.embeds[movieName]);
}
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return (document.getElementById(movieName));
}
}
Then a flashObj.gotoAndPlay(); should work

In your flash file add a callback:
//**updated**
if (ExternalInterface.available)
{
trace("ExternalInterface= " + ExternalInterface.available);
flash.external.ExternalInterface.addCallback("myExternalMethod", null, myFunction);
}
function myFunction()
{
gotoAndPlay("myLabel");
}
In your javascript:
function executeFlash()
{
//**updated**
alert('JS call works fine!');
getObjectById("myFlashID").myExternalMethod(); // myFlashID = your SWF object ID
}
function getObjectById(objectIdStr) {
var r = null;
var o = getElementById(objectIdStr);
if (o && o.nodeName == "OBJECT") {
if (typeof o.SetVariable != UNDEF) {
r = o;
}
else {
var n = o.getElementsByTagName(OBJECT)[0];
if (n) {
r = n;
}
}
}
return r;
}
In your HTML (give your SWF object embedded in your HTML an id = myFlashID):
<a href="" id="flashTrigger" onclick="executeFlash()" />
Here is where you find the documentation for ExternalInterface http://flash-reference.icod.de/

I have managed to crack the problem by using SWFObject
Accessign the flash objects method was easy through SWFObject.js, however it just wasnt working without it. Not sure why though.
All the above suggestions work with SWFObject.js, but none seemed to work without it
Cheers for everyones suggestions
Andy

Related

Looping in PhoneGap-NFC function

I have a problem in a function PhoneGap-NFC plug-in Intel XDK.
function nova_pulseira(cli_nova_id) {
nfc.addTagDiscoveredListener(function (nfcEvent) {
var tag = nfcEvent.tag;
var = TagID nfc.bytesToHexString(tag.id);
if(TagID! == 0) {
nova_pulseira_input(cli_nova_id, TagID);
} else {
myApp.alert( 'error in reading the bracelet.' 'Notice');
}
});
}
The nfc.addTagDiscoveredListener function is used for reading NFC TAG when occurs nfcEvent.
In the first reading it works normally, but when make the second reading, the nfc.addTagDiscoveredListener function is applied two times, the third reading, it is applied 3 times, and so on.
The only way I found to "stop" this function is using location.reload(); but he returns to the Application home page, and the ideal would be to activate a subpage.
I would, somehow, that nfc.addTagDiscoveredListener function is disabled after applying the nova_pulseira_input(cli_nova_id, TagID); function.
PS: I've used
-> Return false;
-> $ .each (Nfc, function () {this.reset ();});
-> Intel.xdk.cache.clearAllCookies ();
-> $ .ajaxSetup ({Cache: false});
Thanks for the help of all ...
Put the function inside a var and redefine it later:
var tagHandler = function () {
handlerOk();
};
function handlerOk () {
console.log("handlerOk()");
tagHandler = function() {
console.log("disabled..")
};
}
function tag() {
console.log("tag()");
tagHandler();
}
tag();
tag();

Reinitializing code in Meteor

I started writing a Meteor app today, for an eCommerce site. This is some code in my basic.js file.
Router.route("/shop", function () {
this.layout("shop");
this.render("catalog");
});
When /shop is rendered, some code inside of shop.js is executed:
Template.shop.rendered = function () {
if ($("figure").hasClass("selected")) {
var
productSelected = $("figure.selected"),
productPrice = productSelected.data("price"),
productTitle = productSelected.data("product"),
productLocation = productSelected.find("a").attr("href");
$(".product-title").html(productTitle);
$(".product-cost").html(productPrice);
$(".content__info__title--overview").find("a").attr("href", productLocation);
}
// Slideshow
var galleryItems = $(".content").children("section");
galleryItems.each(function () {
var container = $(this);
// Update slider when user clicks on the preview images
container.on("click", ".move-down, .move-up", function (event) {
event.preventDefault();
if ($(this).hasClass("move-down")) {
nextSlide(container);
} else {
prevSlide(container);
}
if ($(this).hasClass("selected")) {
var
productPrice = $(this).data("price"),
productTitle = $(this).data("product");
$(".product-title").html(productTitle);
$(".product-cost").html(productPrice);
}
});
});
// Next Slide
function nextSlide(container, n) {
var visibleSlide = container.find("figure.selected");
if (typeof n === "undefined") {
n = visibleSlide.index() + 1;
}
$("figure.selected").removeClass("selected");
$(".content__products figure").eq(n).addClass("selected").removeClass("move-down").prevAll().removeClass("move-down move-up").addClass("hide-up").end().prev().removeClass("hide-up").addClass("move-up").end().next().addClass("move-down");
}
// Previous Slide
function prevSlide(container, n) {
var visibleSlide = container.find("figure.selected");
if (typeof n === "undefined") {
n = visibleSlide.index() - 1;
}
$("figure.selected").removeClass("selected");
$(".content__products figure").eq(n).addClass("selected").removeClass("move-up hide-up").nextAll().removeClass("hide-up move-down move-up").end().next().addClass("move-down").end().prev().removeClass("hide-up").addClass("move-up");
}
});
Now, this works perfectly fine when the app loads, but when I visit a different route and come back, the none of the code in shop.js work. I'm not sure if I'm doing something wrong, but I'd love some pointers.
Change the template rendered from this
Template.shop.rendered
to
Template.catalog.rendered.
Since you want to render the catalog route not the layout template.
btw i was asking about the meteor version because on new 1.0.4 meteor version Template.shop.rendered = function () {} got deprecated instead now we use Template.tabletsList.onRendered(function() {});, try it run meteor update.
Ethaan answered my question in the comments. I just had to change Template.shop.rendered to Template.catalog.rendered. Good grief!

Using $_GET with Jquery

I currently have the following code on my website:
$(document).ready(function() {
$("#contact").on("click", function(e)
{
e.preventDefault();
$("#contactform").toggle('fast');
});
});
I would like to have an if(isset($_GET['email')); trigger this function as well, so have it open on page load if the $_GET variable is set.
I'm rather new with Jquery and not sure if this is possible, I also have another somewhat related question, I'm not sure if I should make a new question for this as I'm fairly new to stackoverflow as well, but here it is.
Say I have two of these:
$(document).ready(function() {
$("#contact").on("click", function(e)
{
e.preventDefault();
$("#contactform").toggle('fast');
});
});
$(document).ready(function() {
$("#archivestop").on("click", function(e)
{
e.preventDefault();
$("#archives").toggle('fast');
});
});
I want one to close if the other one is opened, how would I go about this?
Thanks!
Here's the Javascript-solution:
function getParam(key) {
var paramsStr = window.location.search.substr(1, window.location.search.length),
paramsArr = paramsStr.split("&"),
items = [];
for (var i = 0; i < paramsArr.length; i++) {
items[paramsArr[i].split("=")[0]] = paramsArr[i].split("=")[1];
}
if (key != "" && key != undefined) {
// return single
if (items[key] != undefined) {
return items[key];
} else {
return null;
}
} else {
// return all (array)
return items;
}
};
if (getParam("email")) {
// ...
}
Regarding your second question you can use the following to determine if an element is visible:
var bool = $('.foo').is(":visible");
So to hide an element if it is visible you would do something like this:
if ($('.foo').is(":visible")) {
$('.foo').hide();
}
I'm silly and have answered my first question. I still have yet to have my coffee.
The following works, just insert it into the div that is to be displayed:
<div id="contactform" style="<?php if(isset($_POST['email'])) echo "display:block;" ?>">
content
</div>

Why this won't be fired?

I tried to make a button to start playing video on player.
Player is showing up and works fine if I press "Playing" on control bar.
But My a href link to start playing won't work.
play() won't be fired. why?
<script>
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
}
function play() {
if (ytplayer) {
ytplayer.playVideo();
}
}
</script>
<script type="text/javascript" src="swfobject.js"></script>
<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
<script type="text/javascript">
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/P5_GlAOCHyE?enablejsapi=1&playerapiid=ytplayer",
"ytapiplayer", "425", "356", "8", null, null, params, atts);
</script>
Play
Talking to Flash objects can be difficult. Here's the code I use to send messages:
function runFlashFunction( flashName, funcName ) {
var
od = document[flashName],
ow = window[flashName];
// flash isn't happy with o=func;o(); syntax
try {
if( od && !!(od[funcName]) ) {
return document[flashName][funcName]( );
}
if( ow && !!(ow[funcName]) ) {
return window[flashName][funcName]( );
}
if( ow && ow.length && !!(ow[0]) && !!(ow[0][funcName]) ) {
return window[flashName][0][funcName]( );
}
return void 0;
} catch( ex ) {
return void 0;
}
}
Why all that? because some browsers use different ways to access the flash content. The last one probably isn't needed, but I include it to be sure.
Also your link uses a style which isn't recommended. This is better:
Play
Or even better: assign the callback in the JavaScript, not the HTML:
$(function(){
$('#playLink').click(function(){ // name the link playLink (or whatever)
play();
return false;
});
});
Try not to use a global variable
var ytplayer = null;
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
}
function play() {
if (ytplayer) {
ytplayer.playVideo();
}
}

javascript namespace issue jsfiddle

I have code here http://jsfiddle.net/morenoh149/HWbuv/1/
this should be an easy fix. Why is itemClicked not being found?
my html
13" tortilla<input id="1" type="checkbox" onClick="itemClicked()">
<a id="console">the</a>
the relevant javascript
function itemClicked(element, id) {
if (element.checked) {
list.push(item[id]);
addItem(item[id]);
} else {
for (x in list) {
if (list[x].id == id) list.splice(x, 1);
}
subtractItem(item[id]);
}
var e = document.getElementById("console");
e.innerHTML = "blah";
}
jsFiddle puts the code inside window.onload handler function, so if you declare a function in the Javascript panel it isn't global.
Make it global by assigning it to window:
function itemClicked(element, id) {
/...
}
window.itemClicked = itemClicked;
Or alternatively:
window.itemClicked = function(element, id){...}

Categories

Resources