I found how to call actionscript from javascript, but I need to pass some arguments too (dynamic),how can I do this?
TIA.
Please, try this:
ExternalInterface.addCallback("sendMsg", generateMsg);
function generateMsg(str):void {
trace(str);
}
JS:
msg = "";
function setMsg(myMsg) {
msg = myMsg;
SendDataToFlashMovie(myMsg);
}
In my experience you have to call the function on the flash object.
I use the following javascript function to get the flash object
function GetSWF(id) {
if (window.document[id] != null)
if (window.document[id].length == null)
return window.document[id];
else
return window.document[id][1];
else
if (typeof(document[id]) == 'undefined')
return $('#'+id)[0];
else
if (document[id].length == null)
return document[id];
else
return document[id][1];
}
then call the function as follows
var flash = GetSWF('idOfSWF');
if (typeof flash.sendToActionScript === 'function'){
flash.sendToActionScript(yourObject,orParameter);
}
the AS3 would look like follows
if (ExternalInterface.available){
ExternalInterface.addCallback("sendToActionScript",receivedFromJavascript);
}
function receivedFromJavascript(myObject:Object,myParameter:String):void{
// Do something
}
Hope this helps.
EDIT:
Just noticed that I have a small usage of jQuery in the GetSWF function. I'll take a look and try and remove that. (Its the line return $('#'+id)[0];)
Related
I have jQuery extension written like this:
$.fn.GetBootstrapDeviceSize = function() {
var cMobileSize = $('#users-device-size').find('div:visible').first().attr('id');
return (cMobileSize === "xs" || cMobileSize === "sm");
}
For calling this I use:
$().GetBootstrapDeviceSize()
Is there a way to call this same function like this:
$.GetBootstrapDeviceSize()
Or perhaps even like this:
$.GetBootstrapDeviceSize
Is there a way to call this same function like this:
$.GetBootstrapDeviceSize()
Yes, put it on $, not on $.fn:
$.GetBootstrapDeviceSize = function() {
// ^^
var cMobileSize = $('#users-device-size').find('div:visible').first().attr('id');
return (cMobileSize === "xs" || cMobileSize === "sm");
}
Or perhaps even like this:
$.GetBootstrapDeviceSize
Yes, but it probably wouldn't be best practice. You'd do it by creating a getter for that property on $:
Object.defineProperty($, "GetBootstrapDeviceSize", {
get: function() {
var cMobileSize = $('#users-device-size').find('div:visible').first().attr('id');
return (cMobileSize === "xs" || cMobileSize === "sm");
}
});
Then accessing $.GetBootstrapDeviceSize would run the function and give you the return value. I'd probably just call it BootstrapDeviceSize, though, since you're accessing it as though it weren't a method, so a noun rather than a verb makes sense.
How to check if is function on jquery, but function is in another .js file?
validation.js:
if ($.isFunction('payment')) {
$('[data-numeric]').payment('restrictNumeric');
$('.cc-number').payment('formatCardNumber');
$('.cc-exp').payment('formatCardExpiry');
$('.cc-cvc').payment('formatCardCVC');
}
this is false because func payments is in the payments.js .
Try like this
if (typeof payment === "function")
{
// Do something
}
problem is solved. its works:
if ($.fn.payment) {
//do something
}
Try to check like as follows,
if (typeof payment !== 'undefined' && $.isFunction(payment)) {
$('[data-numeric]').payment('restrictNumeric');
$('.cc-number').payment('formatCardNumber');
$('.cc-exp').payment('formatCardExpiry');
$('.cc-cvc').payment('formatCardCVC');
}
You can check if a function exists using window
For example
var fn = window['NameOfTheFunction'];
if(typeof fn === 'function') {
doSomething();
}
If your function in payment.js is part of a self contained function, you need to set it to so the window object can "see" it by adding this in your self contained function:
window.NameOfTheFunction = NameOfTheFunction;
I'm trying to create a custom function for a google docs spreadsheet. I feel like this is a really simple problem, and I've quickly moved out of my depth.
Please help me. A point in the right direction would be much appreciated.
The googledocs script editor gives this error:
TypeError: Cannot call method "replace" of undefined. (line 50)
For this code:
function replaceGender(name, gender, comment) {
var genderedComment = String();
var name;
var gender;
var comment;
if(gender == "m")
{
genderedComment = ungenderedComment.replace("(name)", name).replace(/\(He\/She\)/g,"He").replace(/\(His\/\Her\)/g,"His").replace(/\(his\/\her\)/g,"his").replace(/\(him\/\her\)/g,"him").replace(/\(he\/\she\)/g,"he");
}
else
{
genderedComment = ungenderedComment.replace("(name)", name).replace(/\(He\/She\)/g,"She").replace(/\(His\/\Her\)/g,"Her").replace(/\(his\/\her\)/g,"her").replace(/\(him\/\her\)/g,"her").replace(/\(he\/\she\)/g,"she");
}
return genderedComment;
};
I think its easy, but I'm doing something wrong.
I've changed the code and it works now without error, but the last.replace(/\(he\/\she\)/g,"she"); and .replace(/\(he\/\she\)/g,"he");
don't replace.?? no idea...
thanks again for all your help... as i said im learning a lot.
here is the code now
function replaceGender(name, gender, comment) {
if(gender == "m")
{
comment = comment.replace(/\(name\)/g, name).replace(/\(He\/She\)/g,"He").replace(/\(His\/\Her\)/g,"His").replace(/\(his\/\her\)/g,"his").replace(/\(him\/\her\)/g,"him").replace(/\(he\/\she\)/g,"he");
}
else if(gender == "f")
{
comment = comment.replace(/\(name\)/g, name).replace(/\(He\/She\)/g,"She").replace(/\(His\/\Her\)/g,"Her").replace(/\(his\/\her\)/g,"her").replace(/\(him\/\her\)/g,"her").replace(/\(he\/\she\)/g,"she");
}
return comment;
};
Several problems actually, aside from the undefined error. You don't want to declare those variables at the top of the function, since what you need is already passed into the function.
function replaceGender(name, gender, comment) {
var genderedComment;
if(gender == "m")
{
genderedComment = comment.replace("(name)", name).replace(/\(He\/She\)/g,"He").replace(/\(His\/\Her\)/g,"His").replace(/\(his\/\her\)/g,"his").replace(/\(him\/\her\)/g,"him").replace(/\(he\/\she\)/g,"he");
}
else
{
genderedComment = comment.replace("(name)", name).replace(/\(He\/She\)/g,"She").replace(/\(His\/\Her\)/g,"Her").replace(/\(his\/\her\)/g,"her").replace(/\(him\/\her\)/g,"her").replace(/\(he\/\she\)/g,"she");
}
return genderedComment;
};
Your variable named ungenderedComment is not defined.
You can test to see if a variable is undefined like this:
if (typeof someVariable === 'undefined') {
alert("variable is undefined");
}
Or like this:
if (! someVariable) {
alert("variable is either undefined, null, false, zero, or some falsey value");
}
EDIT: as the comments point out, it looks like you're using the wrong variable altogether!
I was wondering, can you create a function with an optional parameter.
Example:
function parameterTest(test)
{
if exists(test)
{
alert('the parameter exists...');
}
else
{
alert('The parameter doesn\'t exist...');
}
}
So if you call parameterTest() then the result would be a message "The parameter doesn't exist...". And if you call parameterTest(true) then it would return "the parameter exists...".
Is this possible?
This is a very frequent pattern.
You can test it using
function parameterTest(bool) {
if (bool !== undefined) {
You can then call your function with one of those forms :
parameterTest();
parameterTest(someValue);
Be careful not to make the frequent error of testing
if (!bool) {
Because you wouldn't be able to differentiate an unprovided value from false, 0 or "".
function parameterTest(bool)
{
if(typeof bool !== 'undefined')
{
alert('the parameter exists...');
}
else
{
alert('The parameter doesn\'t exist...');
}
}
In JavaScript, if you neglect to give a parameter it will default to undefined.
You could try it out for yourself easily enough, either in your browser console or using JSFiddle.
You can check for the existance of the parameter, as you say, and that way write a function that can use a parameter or not. However, JavaScript Garden (a great resource) recommends staying away from typeof in most other cases, as its output is just about useless (check out the table of results of typeof).
init default values if not exists:
function loadDialog(fn, f, local, anim) {
switch (arguments.length) {
case 1: f=null;
case 2: local=false;
case 3: anim=false;
}
...
}
best way to check: if the param is not undefined
function parameterTest(param) {
if (param !== undefined)
...
the param could be also a variable or a function name
function parameterTest(p) {
if ( p === undefined)
alert('The parameter doesn\'t exist...');
else
alert('the parameter exists...');
}
None of these answers were doing it for me. I needed to know if someone had used the parameter or not. If someone invoked the function passing undefined I wanted to treat that as them using that value as the parameter. Anyway, the solution was quite easy using some modern JS:
function parameterTest(...test) {
if (test.length) {
return `exists`;
} else {
return `not exists`;
}
}
parameterTest(123); // exists
parameterTest(undefined); // exists
parameterTest(); // not exists
parameterTest(window.blah); // exists
For older browsers you can use arguments:
function parameterTest() {
if (arguments.length) {
return "exists";
} else {
return "not exists";
}
}
null == undefined is true
if (arg == null){
// arg was not passed.
}
Code example:
var button = document.querySelector("button");
function myFunction(arg){
if(arg == null){
alert("argument was not passed.");
} else {
alert("argument " + arg + " was passed.");
}
}
<button onclick="myFunction('foo');">click to fire function w arg</button>
<br><br>
<button onclick="myFunction();">click to fire function w/o arg</button>
I know this is old, but this is my preferred way to check, and assign default values to functions:
function testParamFunction(param1, param2) {
param1 = typeof param1 === 'undefined' ? null : param1;
param2 = typeof param2 === 'undefined' ? 'default' : param2;
// exit if the required parameter is not passed
if (param1 === null) {
console.error('Required parameter was not passed');
return;
}
// param2 is not mandatory and is assigned a default value so
// things continue as long as param1 has a value
}
JavaScript newbie here, I was going through some js code at work when i came across a helper function for object creation, which went like this
createElement = function(name, data){
if(name == TYPES.TEXT){
return new Text(data);
}
else if(name == TYPES.WORD){
return new Word(data);
}
else if(name == TYPES.PARAGRAPH){
return new Paragraph(data);
}
else if(name == TYPES.TABLE){
return new Table(data);
}
<list goes on and on and on... >
}
while this does get the job done i would like to know if there is a better, cleaner way of writing this.
You're right, excessive if..then or switch logic is a code smell and can almost always be refactored into something more elegant. In this case, a factory based upon a name can be refactored into a dictionary with key as that name and value as the function to return
var dictionary = {};
dictionary[TYPES.TEXT] = Text;
dictionary[TYPES.WORD] = Word;
dictionary[TYPES.PARAGRAPH] = Paragraph;
dictionary[TYPES.TABLE] = Table;
createElement = function(name, data){
return new dictionary[name](data);
}
Live example: http://jsfiddle.net/KkMnd/
EDIT: That line in the createElement method could/should first check that something is configured for the TYPES.* passed in. A good way is to check that there is an element in the dictionary before trying to call that method.
return (typeof dictionary[name] == 'function') ? new dictionary[name](data) : some_default_value;
It would be a bit cleaner but semantically the same to use a switch statement.
function createElement(name,data){
switch(name)
{
case TYPES.TEXT:
return new Text(data)
break;
case TYPES.WORD:
return new WORD(data)
break;
default:
// etc. code to be executed if no values match
}
}