I tried to run below code but chrome showed Unexpected token ILLEGAL .
Can anyone help ?
When I click the button, it should add the tag, and when it find images/1h.jpg it does not exist ,it should trigger onerrorevent and run function onc() to add some CSS and load another picture, but it didnt run function onc() and when I access console ,it shows Uncaught SyntaxError: Unexpected token ILLEGAL .
<button id='a' onclick="onc();">aaa</button>
<div></div>
function onc(){
var path1= 'images/1h.jpg';//Does not exist
var path2= 'images/1s.jpg';//exist
var tmp = "<img src='"+path1+"' onerror='nofind(this,"+path2+");' /> ";
$('div').html(tmp);
}
I searched some answers, it seems caused by "" or '', but I really don't know how to fix my code. Thanks.
In the rendered JavaScript in the HTML attribute text, the string you have in path2 won't be in quotes, it'll look like this:
onerror='nofind(this,images/1s.jpg);'
One solution is to put it in quotes:
var tmp = "<img src='"+path1+"' onerror='nofind(this,\""+path2+"\");' /> ";
// Change is here -----------------------------------^^---------^^
...which renders like this:
onerror='nofind(this,"images/1s.jpg");'
A better solution, though, would be not to use an onXyz attribute at all. There's no reason to. Instead, use modern event handling:
function onc(){
var path1= 'images/1h.jpg';//Does not exist
var path2= 'images/1s.jpg';//exist
var img = $("img").on("error", function() {
nofind(this, path2);
}).attr("src", path1);
$('div').html(img);
}
Note that we hook the error event before setting the src attribute. This may not be strictly necessary with error depending on the reason for the error, but it's a good habit to get into and it can matter quite a lot for the related load event. (It's a common misconception that you can hook the load event after setting src; it works most of the time, but may not if the browser has the image cached and the cache settings for it allow the browser to reuse it without revalidating it, in which case the browser may fire the load event as soon as src is set and, not seeing any JavaScript handlers, doesn't queue them to run when the JavaScript thread is next free to run handlers. [There is only one main JavaScript UI thread, but browsers are multi-threaded.])
Related
I want to read require files dynamically based on condition say
var clientName= "something";
var file = require('../somepath/ + clientName);
but I want to ask what will it return if the path doesn't exist?
I tried to find using debugger but it goes berserk and ends the debugging!
It will end with Not Found error. This error will be raised also by the require. You can catch this errors globaly. Check this url http://requirejs.org/docs/errors.html#requireargs
You can override the default error handler of requirejs and play around with it to see what is happening
require.onError=function customErr(err){console.log('Custom:'+err)};
require(['../somedummyfile'])
I have a complex function that loops through the elements during the onSuccess: of my js and I'm getting the following error that I haven't seen before.
exception encountered : {"message": "Invalid argument.", "description": "Invalid argument.", "number": -2147024809, "name": "Error"}
The js function looks like this:
if(Object.isArray(list)){
list.each(function(listItem, index){
if(!Object.isUndefined(listItem.disabled)){
listItem.disabled = disableFlag;
}
});
}
that is called from the onSuccess: portion of an Update. My html is a button that is calling the noted function from an onclick. When I run it the onException: always happens and I'm getting the error by:
Object.toJSON(exception)
Has anyone seen this before? I have tried playing around with the functionality and it seems that when I use the button to do what it's supposed to do after a specific sequence of events is the only time this happens. So, I placed an arbitrary link on the page and wanted to see if I clicked that, what would happen and it updated the JSON object on the page and allowed for me to use the button for it's set action without the error. Any help would be appreciated.
Most of the time this kind of error happens in IE when you set an attribute of a DOM element.
If listItem is DOM element then maybe it's not yet added to the document or disableFlag is an invalid value. Or the error happens outside the provided code.
I have been trying to figure out this particular problem in my developer tools, but I've had no luck thus far. I have an error on one of my js files that says
Uncaught TypeError: Cannot read property 'value' of null
The following error refers to the 1st variable of dt_version below. The particular thing is if I comment out the first line of code. I get the same error on the following variables of offload1 and offload2. The variable is a number that I am trying to get passed over. I run this function on my body when the page loads...onload=updatetotal();
function updatetotal() {
var dt_version = document.getElementById("dt_version").value-0;
var offload1 = document.getElementById("capacity_offload1").value-0;
var offload2 = document.getElementById("capacity_offload2").value-0;
var offload3 = document.getElementById("capacity_offload3").value-0;
}
If a run an if statement looking for document.getElementByID("dt_version");...it defaults to false..so its not being carried over though on the previous page, I can see its input fine with the value in it. What am I missing here guys?
This error means that the id dt_version does not exist. Check your html to make sure it is there:
var dt = document.getElementById("dt_version");
if (dt){
// do your stuff
}else {
console.log("dt does not exist")
}
Another cause for this error may be- as you are calling the javascript function on page load there is a possible chance that your control is not yet completely rendered to the page. A simple solution is just move that control to the beginning of the page. If it doesn't work then an reliable solution is, call the function inside jquery $(document).ready().
I've made a piece of code in jquery that assigns a href attribute to a variable. Here's that code:
$('#reactions' + i).attr('href', 'javascript:comments ('+entry.url+','+i+');');
This should assign a call to the javascript function comments. Now I want to use that call on a jquery mobile button, like this:
document.write('Reactions');
But doing this gives me a in FF and Chrome. This is the error from FF±
Uncaught exception: ReferenceError: Undefined variable: i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what
Error thrown at line 1, column 0 in javascript:comments (i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what,1);:
comments (i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what,1);
In this, i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what is the value of entry.url.
I'm just not getting why this error appears, as far as I know, everything should work.
I know that there are questions looking similar to mine, but I couldn't figure out the answer. If you want to see the whole source, it's here.
Surround entry.url with quotes:
$('#reactions' + i).attr('href', 'javascript:comments ("'+entry.url+'",'+i+');');
The best way to fix the issue is to do it the "jQuery way". Instead of adding a href attribute that executes JavaScript, add a click event:
$('#reactions' + i).click( function() {
comments( entry.url, i );
});
Similarly don't use document.write() but add elements to the document using jQuery functions.
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.