I'm working on a project which needs to use youtube api. The html and javascript works fine in different browsers. Yet When I just WPF's webbrowser to run it, I get the following error:
Script Error
line:1
char:1
Error: Syntax Error
Code:0
URL about://:/
Do you want to continue running scripts on this page?
yes no
When I click yes, the message box went away and the program just works fine. I comment out most of the content on the html file and found that the following sentence caused this problem:
google.load("swfobject", "2.1");
So now, I just want to catch this error, just don't want the messagebox to popup. I tried
try{
google.load("swfobject", "2.1");
}catch(err)
{alert("caught");}
Yet, I still get the initial error box. Is there anyway to remove the error box? Thank you very much.
You can't actually use try/catch blocks to catch syntax errors. A syntax error means that the parser can't correctly parse the JavaScript. Syntax errors happen before the code is ran.
Related
I am running a Tampermonkey script on a website that I do not have the code for.
Sometimes it happens that I have a value that does not exist on the page and I get the following error:
"Cannot read property 'click' of null"
And the entire script stops. How can I tell get my script to ignore the error and just carry on to the next line of code?
Here is an example of a vanilla Javascript line that I work with:
document.querySelector('[value="xyz"]').click();
Only execute click() if the selector found something:
if(document.querySelector('[value="xyz"]'))
document.querySelector('[value="xyz"]').click();
You can't, and you shouldn't want to: errors are bad. They're not informative, they are a signal that the code has run into an unrecoverable error and the current code path should be terminated. If you were to ignore it, and keep running, now you're in a state where any subsequent line is just as likely to also throw an error.
Either actually fix things, by making your tampermonkey script not interfere with the way the page it's running on builds its DOM, or as a last resort, you can find out which function is throwing the error for the specific page(s) you're running into this, and then _specifically for those pages, find and rebind the entire function using a try/catch, such as:
const _old_fn = window.theFunctionInvolved;
window.theFunctionInvolved = function(...args) {
try { return _old_fn(...args); }
catch (e) {}
};
But of course, all you've now done is moved the buck: you'll have effectively guaranteed different errors later on, with the actual cause now permanently hidden.
So really: don't do this. Fix your tampermonkey script, or stop using it altogether.
I'm using Google Analyticator and my website is http://gestionalemagazzinoonline.it/
You can see the error in the javascript console.
I think that is the suspected file:
https://github.com/wp-plugins/google-analyticator/blob/master/google-analyticator.php
The problem is also if i disable the plugin, or if i change the source code i don't see the changes (maybe there's a cache?).
How can I fix?
Script tags are meant to write javascript in them, and not html.
You have a script block inside a script block. Remove that, and it will work all fine.
I get the following error in Firebug for my JavaScript application:
SyntaxError: missing ; before statement when adding new script
The error relates to Google Analytics, but I only get the error when I add another script using RegisterStartupScript. I've had same error point to the script below whilst testing too, so I think the Google thing is a red herring.
Here is the script that gets rendered:
<script src="https://www.thedomain.com/pixel/confirmationjs?pthru=1055|1|pixeltest|27902471&dlp=1.20">
</script>
How do I solve this problem in ASP.NET with RegisterStartupScript?
When getting your technical info of a salesman, never accept what he says to be the complete story or the whole truth!
Turns out that the pixel image I was testing against was single use only, so it worked first time and gave an error on subsequent calls.
I know this is a little to less to get an answer on what the problem is so what I ask is how to debug it.
I get the following error (the image below). No line, script or anything specified. Also except the ones in jQuery and raphaeljs libraries I don't have any custom error handler defined.
Got any ideas on how to debug this?
(The main script for example has around 3k lines and since I don't know where the error occurs I don't know witch part of it to post. I need only a way to find that.)
Thank you for your time.
This happens when the script throws a string, rather than a proper exception, like:
throw 'Error in protected function: )55';
See this other SO question for possible solutions:
How can I get a Javascript stack trace when I throw an exception?
Try chrome. Webkit can provide stack traces:
Web Inspector: Understanding Stack Traces
Sample:
<script>
function i2(){
throw "CustomError";
}
function invoke(){
i2();
}
</script>
<button onclick="invoke()">yo</button>
local function ensureAnimDict(animDict)
if not HasAnimDictLoaded(animDict) then
RequestAnimDict(animDict)
while not HasAnimDictLoaded(animDict) do
Wait(0)
end
return animDict
end
I'm trying to make a PoC of reflected Cross-Site Scripting on a website that I'm testing right now. I've found a place inside of a Javascript code where commands can be injected, however the trouble is that there the previous block of code throws a 'not defined' error and therefore (at least I think so) my injected code is not executed. Is there any chance to execute the code anyway?
Here is the code:
UndefinedObject.Init({
Var1:"a",
Var2:"b",
Var3:"can_be_injected_with_JS_code")}
I can't inject any HTML tags as these are filtered by the application.
Many thanks!
Wrap them under try catch block.
In a sequence of execution, if the code fails, the remaining part will not be executed. Javascript errors ("Exceptions") can be caught using try...catch (if you are able to inject this try - catch also).
If there is a different flow (via another event), the code will continue.
You can either try using a try-catch, or if that won't help, try using window.onerror
Generally the right way of doing that is using try-catch-finally or try-finally:
If you make something about the error - log or do something else. Catch may be also used to execute your code, but not a good practice. You can do nothing about the error if you want, that`s why finally is used.
Finally is used when it is important to execute a piece of code, no matter if an error is thrown or not. For example in C++ or other language when you work with files inside finally the file is closed ( you can not leave it opened ). Look here for some examples.