Unexpected identifier for function call - javascript

I have a simple onclick feature which calls a function but im getting an unexpected identifier but i don't see where the mistake is, it doesn't actually give me the error until i click the link which is confusing.
This is the script:
function discard_item(id,name){
alert('test');
return false;
}
function load(){
name = 'test';
id = 1;
output = [];
output.push('[Discard]');
document.getElementById('main').innerHTML = (output.join(''));
}
load();
When i click the link to call discard_item i get : Unexpected token }
Can't seem to see a mistake in my script though ? http://jsfiddle.net/Lu2HK/6/
Hope you can help!

Try this:
Change the " to escaped 's:
output.push('[Discard]');

Related

Javascript function keep asking for argument

First I made function:
function createComment(template) {
let emoji = document.getElementById("emojis").value;
let comment = template.replace(/p/g, emoji)
document.getElementById("comment").innerHTML = "kok" ;
}
and in HTML button : <button type="button" onclick="createComment()">Go</button>
but, I wanted to remove template argument from function because button didn't work so I just reduced JS function to :
function createComment() {
//let emoji = document.getElementById("emojis").value;
//let comment = template.replace(/p/g, emoji)
document.getElementById("comment").innerHTML = "kok" ;
}
but even now developer tools report error Uncaught TypeError: Failed to execute 'createComment' on 'Document': 1 argument required, but only 0 present.
If I change function name to anything to anything else like from createComment to createCommentS and in button too it works. For some reason it just keeps on hanging of original function argument and it's stubborn doesnt wanna give up on asking argument. Why?
Rename the function to something else because it is probably being shadowed by document.createComment()

Cannot read property of undefined in class

I'm new to javascript so can anyone help me figure out why this code is not working?
I have a class and it calls a cordova barcode scanning function. I've got an example that works, however I want to be able to separate out the function(result) and function(error) and use onSuccess(result) and onFailure(error).
I have no idea why this is happening so if anyone can help that would be great.
EDIT: so ive updated the code based on Stradosphere said however im still getting result is not defined errors.
Full error message:
Uncaught ReferenceError: result is not defined at barcodeScanner.scanBarcode (barcodeScanner.js:10) at HTMLButtonElement.myFunction (main.js:18)
var me = this;
class barcodeScanner {
constructor() {
this._barcodeResult = 0;
}
scanBarcode() {
//THIS THROWS result is not defined error
cordova.plugins.barcodeScanner.scan(me.onSuccess(result), me.onFailure(error));
//THIS WORKS
cordova.plugins.barcodeScanner.scan(
function (result) {
me._barcodeResult = result.text;
alert("Barcode Scanned:" + me._barcodeResult);
},
function (error) {
alert("Scanning failed: " + error);
}
);
}
onSuccess(result) {
this._barcodeResult = result.text;
alert("Barcode Scanned:" + this._barcodeResult);
}
onFailure(error) {
alert("Scanning failed: " + error);
}
}
Looking at the docs, it appears that cordova.plugins.barcodeScanner.scan() expects you to pass a function into it. But you are calling it like this:
cordova.plugins.barcodeScanner.scan(me.onSuccess(result), me.onFailure(error));
This is passing the result of the function .onSuccess(result), but result is not defined, so you are getting an error. Additionally, you want this to be the class instance, but by defining me as this outside the class, me won't equal the class instance like you want it to. But you don't need it anyway.
Try passing functions in instead:
cordova.plugins.barcodeScanner.scan((result) => this.onSuccess(result),(error)=> this.onFailure(error))
Maybe a scope issue on your use of this. Try:
var me = this; //(put this at class level)
cordova.plugins.barcodeScanner.scan(me.onSuccess, me.onFailure);

SCRIPT1003 error in IE

In my project, I make a require call to a js file to create an object of the file.
For example,
require(['directory/hello-js-file'], function(Hello){
var hello = new Hello();
hello.show();
});
After this block, Hello is undefined. In the IE console, I see this error message, 'SCRIPT1003: Expected ':' File: Function code (13), Line:2, Column:1'
The function looks like this:
function anonymous(){
return eval(arguments[0]);
}
I was able to narrow down this issue being originated from two functions (Contents have been modified but the logic is same.):
bindDetail: function($view){
var self = this;
var details = object.details; // object is a global variable
for(var i = 0; i < details.length; i++){
if(self.hasData(details[i])){
// doSomething
}
}
},
hasData(detail){
if(detail.data !== undefined && detail.data !== ""){
return true;
}
return false;
}
This issue only arises in IE. This works fine in Chrome.
Can someone please direct me to a solution? Thank you. I appreciate your help.
After posting the question, I realized that I was missing the :function tag to hasData. That resolved the issue. :)

Pass a Parameter to Another Function and Into innerHTML

I've built my own lightbox, and it's working rather well. I built my own because I needed it to be without a framework, and also work well within a game I'm building. However, I've run into a problem I'm fairly certain is simple, but proving rather vexing to me. The issue I'm having is taking the parameter "slideName" and passing it through to the "fillRightButton()" function.
var createSlidebox = function(cancelButton, bannerImg, slideName) {
fillRightButton("nextSlide",slideName);
};
Here's a portion of that function:
var fillRightButton = function(rightButtonType, rightDestination) {
if (rightButtonType === "nextSlide") {
document.getElementById("lightbox_fright").innerHTML = '<a onclick="changeSlide(' + rightDestination + ')">Next</a>';
}
}
The "fillRightButton()" function performs fine when it is called directly, and this code works if you put the parameter in directly:
var createSlidebox = function(cancelButton, bannerImg, slideName) {
fillRightButton("nextSlide", "mySlideName");
};
However, without the quotes it renders as:
<a onclick="changeSlide([object Object])">Next</a>
with a "Uncaught SyntaxError: Unexpected identifier" JS error. How would I fix this? Thanks!
use data attribute to store the object using jQuery, in function call
var fillRightButton = function(rightButtonType, rightDestination) {
if (rightButtonType === "nextSlide") {
document.getElementById("lightbox_fright").innerHTML = '';
// document.getElementById("lightbox_fright").innerHTML = '<a onclick="changeSlide( $(this).data('dest') )">Next</a>';
var a = document.createElement('a');
a['data-dest'] = rightDestination;
a.onclick=function(){changeSlide( this['data-dest']); }
a.innerHTML = 'Next';
document.getElementById("lightbox_fright").appendChild(a);
//document.getElementById('lightbox_fright > a').data({'dest':rightDestination});
}
}
use jQuery if not already included
PS updated w/o jQuery please give it a try let me know if it works, recommend using jQuery though

Can't seem to add js variable to parent.href.location

Dear stackoverflowers,
I would lik to add an variable to the following js line:
setTimeout("parent.location.href = 'http://www.url.com/lists.php?listid=';",2000);
How do I do this? I can't seem to get the ' right.
setTimeout("parent.location.href = 'http://www.url.com/lists.php?listid=' + variable;",2000);
It is always better to pass a function handler to setTimeout(). Moreover, it will easily solve your problem with placing the quotes:
setTimeout(function() {
parent.location.href = "http://www.url.com/lists.php?listid=" + variable;
}, 2000);
Use function to redirect as follow....
function Redirect(val)
{
parent.location.href = 'http://www.url.com/lists.php?listid='+val;
}
setTimeout(" Redirect('"+variable+"')",2000);

Categories