what is this usage of alert in javascript? - javascript

Here is a xss code:
<img src=x onerror="javascript:window.onerror=alert;throw 1">
I can't understand the usage of alert here. Why we don't need parentheses after the alert? And I can't understand the behavior of browser. The browser will pop up a box and dislplay Uncaught 1. It looks like that the browser first pop up an alert box and then fill the exception string into the box. However, I am not quite sure how this happens. BTW, I tested this in chrome.

The window.onerror itself is a function. You can say it as a function name or better, function reference. And alert is also a name of the function, which can be called as funtion reference.
So, they are mapping the onerror with alert, i.e., when the onerror event takes place, there will be an alert.
The window.onerror being an event handler, and alert is something that alerts whatever sent into the parameter, now the onerror event handler sends the event information to the alert and yes, you get what's the error, when an error occurs.
More information about parameters and working of window.onerror. Their syntax is:
window.onerror = funcRef;
Where the funcRef is referred to alert().

Related

how should you pass a context to a named function using jQuery .on?

I have a function I need to call from a few different places, the function runs after clicking an 'a' tag or from a submit event, and in all three instances I need to preventDefault but I get the error 'cannot preventDefault of undefined'.
$('a#Link').on('click', supaFunc);
function supaFunc(e) {
e.preventDefault();
// do all the things...
}
Had a look at the documentation but I haven't solved it. I'm sure its to do with context, and I can make this work buy calling an anonymous function in the .on method that works fine, but I'd prefer not repeat this function three times.
jQuery event handlers should always receive the event as their first argument. If it's undefined, I would suspect that you call the handler in a different fashion at some place.
To find out where and how, try changing the code to
function supaFunc(e) {
if (typeof e === 'undefined') {
debugger;
}
e.preventDefault();
}
Now, when the function is called without an argument, the debugger will kick in.
Open the developer tools of your browser and reload / run the page. When the problem occurs, follow the call stack downwards until you find the location where supaFunc was called without an event as an argument.
Example image of the Chrome Developer Tools debugger.
May be <a id="link"></a> does not exist in all three cases, the error seems point to that.

Neither Alert nor Console.log firing within JSON request

$.getJSON("/example-json.js", function(result){
alert(result);
console.log(result.backgrounds[0].image);
});
Is there any reason the alert or console.log wouldn't work here? I have this method within the History.bind event binder but I do not think that is the issue because I was still not able to get an alert or log the result to the console even after moving the JSON request outside of the event binder.
I know the GET request is firing correctly as I can see it within the console, it is the alert and console.log that for some reason are not working for me.
So I think I found the problem, I was using single quotes within my JSON file and when changing those to single quotes, the alert() and console.log() began firing. I'm not really sure why using single quotes would have an effect on the success function of the JSON method, though?
See if it works like this:
$.getJSON("/example-json.js").done(function(result) {
alert(result);
console.log(result.backgrounds[0].image);
});

get error message from jquery's eventData object

I currently have a javascript error handler like this:
window.onerror = function(msg, url, line){ //stuff }};
But I would like to be able to use jquery to attach to onerror like so:
$(window).error(function(evtData){//stuff});
My question is, from jquery's eventData object, how can i get the error's message, url, and line number, as I did in the non-jquery function?
Thanks in advance.
From jQuery docs.
Note: A jQuery error event handler should not be attached to the
window object. The browser fires the window's error event when a
script error occurs. However, the window error event receives
different arguments and has different return value requirements than
conventional event handlers. Use window.onerror instead.
Refernce: http://api.jquery.com/error/

JavaScript Error Coding

I am having some issues with my homework assignment. I don't know how to start it or how to do it. I don't need the entire code just what is needed for what is being asked. Please can anyone help me on this. I need it ASAP.
Write a custom error handling JavaScript function called processErrors that handles a custom error by assigning it to the onerror event handler. Include the block of JavaScript statements needed to pass the arguments sent by the JavaScript interpreter into the processErrors function, send an alert message with the agreements, return, and write the event handler that calls the processErrors function.
Please can anyone help me.
function handler (processErrors); { onerror="alert ('There was a custom error')"}
This type of stuff can be a bit challenging when you are completely new to it, so Ill help. In an html page, put something like the following
<script>
function handler(event) {
alert(event);
}
</script>
note that example is not complete according to your question. what this does is declare a js function, 'handler', that takes an argument, 'event', and then pops up the event. This is done is 'script' tags, which you should also expand according to your research.
The next thing you will have to do is assign the function defined above that you complete, to the onerror event of some dom, as shown here: http://www.w3schools.com/jsref/event_onerror.asp
Look here for more guidance on js.
http://www.w3schools.com/js/default.asp
You also might want to google things like: "html script tags" and "javascript event handlers"

JQuery error property for error handler

I'm just playing around for the first time with jQuery's ajax functionality. I wanted to add a function that could handle any errors. So, in one of my client javascript blocks, I added the following line:
<script type="text/javascript">
....
$.ajax({ error: function () { alert('boo'); } })
....
</script>
I expected that this would bind the error handler, so that when an error occurs, it would fire the anonymous function included.
What happens instead though, is that it immediately fires the function on page load, as soon as it parses this line of code.
What am I doing wrong? What is the proper way to bind the ajax error handler?
I'm not sure if I understood your question correctly, let me know if I've misunderstood.
I assume that you are trying to create a generic ajax call error handler? If that's the case, you have got the wrong idea.
Are you are just trying to bind the event handler? In this case, you are executing it.
I would recommend you read and check out the examples on these jQuery API reference docs:
API/1.3/Events
Ajax/jQuery.ajax
Also check out the post link provided by F.Aquino and this SO post: JavaScript Exception Handling.
This is could be helpful too: Handling AJAX Errors With jQuery.
You want to change the global settings. Check jQuery documentation.
$.ajaxSetup({
error: function () { alert('boo'); }
});

Categories