Javascript variable function method call - javascript

I apologise if this has been asked elsewhere, I looked but without knowing the name for what I am doing is, I couldn't find anything.
Anyways, the code is as follows:
function alertTypeOptions(AlertType, AlertOptions) {
navigator.notification.AlertType(AlertOptions);
}
This code is for a phonegap / cordova application.
The basic idea is that you pass the function a two variables and these are used to execute the appropriate method. Examples of this could be alertTypeOptions('beep', '3') or alertTypeOptions('vibrate', '2000'). This (should) play the default alert tone 3x or vibrate the phone for 2 seconds.
I am currently getting the following error:
02-21 15:36:07.185: E/Web Console(7206): Uncaught TypeError:
Object #<Object> has no method 'AlertType'
at file:///android_asset/www/res/scripts.js:181
Obviously the function is currently just using the alertType variable as written rather than as a variable.
Is there a way to get this to work elegantly? Currently my only thoughts are to use a switch statement with AlertType as the check.
Jack

It looks like you want to access your function using the bracket notation :
navigator.notification[AlertType](AlertOptions);
or rather, if I trust this documentation and if AlertOptions is an array :
navigator.notification[AlertType].apply(navigator, AlertOptions);

Related

Aframe - call aframe function from another javascript file

I'm trying to turn positional tracking off, and I believe that this function in the aframe code is what I'm after:
checkHasPositionalTracking();
However, in my second js file (which is defined AFTER aframe.js), if I try to call that function, I receive this error:
Uncaught ReferenceError: checkHasPositionalTracking is not defined
You need to call it with full reference to the window.AFRAME browser global, so use AFRAME.utils.device.checkHasPositionalTracking () in your code to access it.

How to call a non-parameterised QML function from C++

I am trying to call a function in my QML:
function resetSomething() {
tempVar= undefined
}
I am not sure how to write the invokeMethod for this function.
I tried using something like that:
QQuickItem* qObj= findChild<QQuickItem>("temp");
if (qObj)
{
QVariant returnArg;
QMetaObject::invokeMethod(qObj, "resetSomething",Q_RETURN_ARG,QVariant,returnArg));
}
But it gives the error
no matching function resetSomething(QVariant) found.
As you can see in your error message Qt's Meta Object System is trying to find method resetSomething(QVariant), but your method doesn't have any parameters. I guess it's because of wrong Q_RETURN_ARG macro usage.
Seems that you can simply fix it like this:
QVariant returnArg;
QMetaObject::invokeMethod(qObj, "resetSomething", Qt::DirectConnection,
Q_RETURN_ARG(QVariant, returnArg));
Also I suggest you to read official Qt documentation, it is brilliant.
BTW: Nice repo from Google with a lot of C++/JavaScript intercommunication. You can find a lot of useful patterns there.

Browser claims JS function is 'not defined,' but if I run it from the command line it works

I'm just getting started with Javascript, trying to build a Jeopardy game, and I'm trying to call a function from within another function. On my site, you click this button:
<input type="button" value="Build My Jeopardy! Game" onclick="drawBoard()">
Which runs this function:
function drawBoard() {
formtoVariables();
document.getElementById('body').innerHTML = '<center><h1>Jeopardy!</h1><div id="screen"><table id="board"><thead><td id="cat1" value="cat1"></td><td id="cat2"></td><td id="cat3"></td><td id="cat4"></td><td id="cat5"></td></thead><tr><td id="r1c1"></td><td id="r1c2"></td><td id="r1c3"></td><td id="r1c4"></td><td id="r1c5"></td></tr><tr><td id="r2c1"></td><td id="r2c2"></td><td id="r2c3"></td><td id="r2c4"></td><td id="r2c5"></td></tr><tr><td id="r3c1"></td><td id="r3c2"></td><td id="r3c3"></td><td id="r3c4"></td><td id="r3c5"></td></tr><tr><td id="r4c1"></td><td id="r4c2"></td><td id="r4c3"></td><td id="r4c4"></td><td id="r4c5"></td></tr><tr><td id="r5c1"></td><td id="r5c2"></td><td id="r5c3"></td><td id="r5c4"></td><td id="r5c5"></td></tr></table></div></center>';
fillBoardWithText();
};
The problem I'm running into is with formToVariables() - the browser gives me this error:
ReferenceError: formtoVariables is not defined
However, if I open up the console in Chrome and type
formToVariables();
the function runs. I think this issue might be related to scope - I can call formToVariables() myself, but drawBoard() doesn't have access to it for some reason. Any idea why that might be? Or is the problem related to something else?
Your t is lowercase instead of capital:
formtoVariables();
Should be:
formToVariables();
Since Javascript is case-sensitive those are two different function names. The second is defined, the first is not.
JavaScript is case sensitive. formToVariables != formtoVariables.

Javascript error - SXBB[id].resize is not a function

My forum (based on phpbb3) has a Javascript error that I'd like to resolve. In FF and IE the following error occurs:
Error: SXBB[id].resize is not a function
Source File: http://digital-diy.com/forum/classes/scripts/select_expand_bbcodes.js
Line: 197
The mod that uses this script is called "Syntax Highlighter 1.0.15". The developer is not sure why the error occurs, hopefully someone at stackoverflow can lend a hand?
Track down the SXBB object or array and view the id variable.
Make sure that property (what id refers to) is set on that object (SXBB).
My guess is it isn't, and it's undefined, and undefined has no resize() method.

variable assignments using jQuery failing in Safari?

I'm using jQuery 1.3.2 and it's breaking under Safari 4 for mysterious reasons.
All of my javascript references are made right before the tag, yet with the following code:
var status = $('#status');
status.change( function(){ /* ... */ } );
The following error is displayed in the Web Inspector:
TypeError: Result of expression 'status.change' [undefined] is not a function.
However the error is not encountered if I eliminate the variable assignment attach the change method directly like so:
$('#status').change( function(){ /* ... */ } );
Why? I need to use variables for this and several other findById references because they're used many times in the script and crawling the DOM for each element every time is regarded as bad practice. It shouldn't be failing to find the element, as the javascript is loaded after everything except and .
Try changing the variable to something other than "status."
It's confusing your variable with window.status (the status bar text). When I typed var status = $('#status') into the debugging console, the statusbar changed to [Object object]. Must be a bug in Safari.
If you put the code inside a function, so that status becomes a function-local variable, it should work.
It's standard practice in jQuery to wrap things in a
$.onready(function() {
});
This makes sure the DOM is loaded before you try to manipulate it.

Categories

Resources