Replace functions in javascript (to shorten phonegap plugins commands) - javascript

I need to shorten my functions (I use them a lot, if i shorten them probably my app needs less storage)
For Example I have navigator.notification.confirm
it is a function. I call it like this : navigator.notification.confirm(a, b, c, [d, e, f, ...])
I want to shorten it as : confirm(a, b, c, [d, e, f, ...])
I used var confirm = navigator.notification.confirm but it doesn't work :(
I can use function confirm(a, b, c, d) { navigator.notification.confirm(a, b, c, d) }
but isn't there any shorter way ?

Here is an example of how I do this for navigator.notification.alert:
I put this in my function that is called from the deviceReady event.
//Shortcut for navigator.notification.alert
if (navigator.notification) {
window.alert = function (message,func,title,buttonTxt) {
if(!func){
func = null;
}
navigator.notification.alert(message, func, title, buttonTxt);
};
}
Now with this code in place, anytime I want to alert something I can do this:
alert('hi');
instead of:
navigator.notification.alert('hi');

Related

Add a script before </body> only on a single component - vuejs

I'm vuejs newbie, and I'm start to use the clearsale api for validations and want make a fingerprint catch for an validation, and need's call a script funcion on page's bottom only in one vuejs component. This is the script in question:
<script>
(function(a, b, c, d, e, f, g) {
a["CsdpObject"] = e;
(a[e] =
a[e] ||
function() {
(a[e].q = a[e].q || []).push(arguments);
}),
(a[e].l = 1 * new Date());
(f = b.createElement(c)), (g = b.getElementsByTagName(c)[0]);
f.async = 1;
f.src = d;
g.parentNode.insertBefore(f, g);
})(window, document, "script", "//device.clearsale.com.br/p/fp.js", "csdp");
csdp("app", "appKey");
csdp("sessionid", "sessionID");
</script>
I called out of export default scope, but thei not recognize the csdp functions.
(p.s. sorry about my english)
You should put this inline script in your public/index.html file - but without the last 2 statements (where you call the csdp function). In your component then you will write
window.csdp("app", "appKey");
window.csdp("sessionid", "sessionID");

Can I modify a Javascript library at runtime?

I use a web based development environment for data entry forms. The environment lets me create rules that are triggered by form events. These events run in js in the browser but there is almost no support for debugging, which makes problem solving a nightmare.
The code in the browser has a central event handler, which has a logging feature but the quantity of information produced by it is so large, it makes finding what you need difficult. Think of info level logging gone mad. Plus you have to open a separate window to access the log.
I need to be able to log certain events to the console, or trigger breakpoints at specified rules. Is there a way to modify the environment's code below to allow it to call my debugger instead of (or in addition) to SFLog?
function handleEvent(n,t,q,r,u,f,e,o,s,h,c,l){
if(eventsCancelled!==!0){
SFLog({type:3,source:"handleEvent",category:"Events",
message:"{2} event fired from {1} - {0}",parameters:[n,t,q]});
var b="Events/Event[#SourceID='"+n+"'][#SourceType='"+t+"'][Name/text()="+q.xpathValueEncode()+"]";
//Rest of the event handler...
function SFLog(n){
if(checkExists(_debug)){var s=translateDebugLevel(n.type);
if(s>=_debug){
varu=n.type,e=n.source,r=n.category,q=n.message,h=n.parameters,o=checkExists(n.exception)? WriteExceptionXml(n.exception):null,t=n.data,l=checkExists(n.humanateData)?
n.humanateData:!0,f=(new Date).format("yyyy-MM-ddTHH:mm:ss:fff");
checkExists(t)&&(dataString=t.xml,checkExists(dataString)||(dataString=t),l===!0&&(dataString=Humanate(dataString)));
//more code for SFLog...
Cleaned Up Code
function handleEvent(n, t, q, r, u, f, e, o, s, h, c, l) {
if (eventsCancelled !== !0) {
SFLog({
type: 3,
source: "handleEvent",
category: "Events",
message: "{2} event fired from {1} - {0}",
parameters: [n, t, q]
});
var b = "Events/Event[#SourceID='" + n + "'][#SourceType='" + t + "'][Name/text()=" + q.xpathValueEncode() + "]";
//Rest of the event handler...
}
}
function SFLog(n) {
if (checkExists(_debug)) {
var s = translateDebugLevel(n.type);
if (s >= _debug)
{
varu = n.type;
e = n.source;
r = n.category;
q = n.message;
h = n.parameters;
o = checkExists(n.exception) ?
WriteExceptionXml(n.exception) :
null;
t = n.data;
l = checkExists(n.humanateData) ?
n.humanateData :
!0;
f = (new Date).format("yyyy-MM-ddTHH:mm:ss:fff");
checkExists(t) &&
(dataString = t.xml, checkExists(dataString) ||
(dataString = t), l === !0 && (dataString = Humanate(dataString)));
//more code for SFLog.
I agree with #Eddie but one solution could be to wrap the logger function and and override it, and only log the events you care about. e.g.:
function SFLog(n){
//old code
}
//run on the console, the first line, and then the second.
var oldLoggger = SFLog;
function SFLog(n) {
if(/*some criteria*/) {
oldLogger(n);
}
}
This way you can run the default logger with different conditions, but it probably would be best if you could modify the logger code itself to accept certain criteria, like, event type to log, or targetElement's ID, class etc.
PD: If you need to modify the eventHandler itself, you should:
remove the event handler first.
create your wrapper function.
add the wrapper function as event handler

SyntaxError: missing { before function body

I want to get our website up and running.
I installed a plugin for a slideshow, which then stopped the page's form automatically refreshing when closing the slideshow. After a lot of research, I put in this code to the jquery.easing.1.3.js
Now, I am able to get an automatic refresh, but my menu doesn't work.
The new error using Firefox says:
SyntaxError: missing { before function body
However, in Chrome, it says:
Unexpected token .
The website is: www.harmandisplay.com
The code in jquery.easing.1.3.js:
// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing,
{
$(document).ready{(function(): {
def: 'easeOutQuad',
swing: function (x, t, b, c, d) {
//alert(jQuery.easing.default);
return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
},
easeInQuad: function (x, t, b, c, d) {
return c*(t/=d)*t + b;
},
easeOutQuad: function (x, t, b, c, d) {
return -c *(t/=d)*(t-2) + b;
},
.... and it goes on and on.
This
$(document).ready{(function(): {
is a syntax error
remove the ':'
You have an invalid : syntax that is breaking the JavaScript code. Change
$(document).ready{(function(): {
To
$(document).ready{(function() {

javascript send object's attributes to function

i have a situation i have been thinking for a while and cant seem to find logic to solve it. hope you can.
i have a series of buttons that i would like to track, an example of one is this:
when a button is clicked a function is called, sending some strings and an object with a series of attributes. different buttons have different sets of attributes.
$(".btn").on('click', function(ev){
trackFunction("Purchase","apply_promo", { product_code: "product1", last_page: "home", refer: "facebook", promo: "12345" });
});
The track function will then receive this and send it to my analytics software
but i need it to be sent in this format
dataToSend = { event_type : eventType, event_value: eventValue, data };
where data is each and every attribute from the object the button is sending.
in that example it woud be something like this
dataToSend = { event_type : eventType, event_value: eventValue, data.product_code, data.last_page, data.refer, data.promo };`
here is my tracking function :
function trackFunction(eventType, eventValue, data ) {
dataToSend = { event_type : eventType, event_value: eventValue, data };
analyze(dataToSend);
return true;
}
the problem here is that not every button is sending the same attributes inside the object so i cant hardcode the output.
hope i made myself clear.
and thank you so much.
Either you add the common properties to the object each time. The downside is that you're altering a function parameter which is generally a bad idea because it might cause weird bugs:
function track(a, b, data) {
data.a = a;
data.b = b;
sendData(data);
}
Or you turn it around by iterating over the object's properties:
function track(a, b, data) {
var send = {a: a, b: b}, k;
for (k in data) {
if (data.hasOwnProperty(k)) {
send[k] = data[k];
}
}
sendData(send);
}
If you use lodash, underscore, or jQuery (or probably every other JS framework out there) you usually have an extend function at your disposal, which does pretty much what you want (and internally the same as the second example).
// with underscore:
function track(a, b, data) {
sendData(_.extend({a: a, b: b}, data));
}

javascript function into variable

Happy Holidays to all.
I'm trying to execute a function that I have saved in a variable, as I could do this?
I tried this.
funcVar = 'function (a, b) {c = a + b; alert (c);}';
document.write (funcVar);
but has no functionality, I want to use that function that is in the variable at some point.
You need to:
Write it as a function, not a string
Call it, not write it to the document as if it were a string of HTML
Pass arguments as it doesn't make sense to add undefined to itself
You should:
Not use globals
Such:
var funcVar;
funcVar = function (a, b) {
var c = a + b;
alert (c);
};
funVar(1,2);
That's not a function, that's a string. You need to remove the quotes. And you need to execute it, not print it:
var funcVar = function (a, b) {c = a + b; alert (c);};
funcVar(23, 42);
This works. Thank you all.
var funcVar;
funcVar = 'function stop() {alert ("Test");}';
eval(funcVar);
stop();

Categories

Resources