I'm working on a Firefox addon that utilizes content scripts to add some additional features to a website. If my content script contains a return statement, the code won't run - not even the "console.log" statement in the first line of the script!
I have tested this on Firefox 68.0.2 (64-bit). The return statement is in an if block, so any console.log statements before it should be executed - but they aren't. If I comment out the "return" line, all lines before and after it work again, but if I leave it in, not even the first statement is executed.
console.log("Starting XX-Tools")
var ctr_player = window.VIDEO_PLAYERJS
if (typeof ctr_player === undefined) {
console.log("No player found")
return
}
console.log("Player found!")
I was expecting:
Starting XX-Tools
Player found
or at least:
Starting XX-Tools
No player found
I got no output. If I remove the return statement, I get the following output:
Starting XX-Tools
Player found
If that code is not inside a function, then return is a syntax error. – Pointy
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_return_or_yield
I placed this bit of code in both atom and the chrome console.
function a() {
alert('A');
return function() {
alert('B');
};
}
a = a();
In the chrome console it works likes its supposed to, the first time it is run it shows A the second time it is run it shows B. However, in the case of using Atom each run shows A and it never switches to b? Why is this?
I have some JavaScript functions like this:
function onSelectRow_${itemid}(){
something;
}
This it is appearing like this in Firebug script tab:
function onSelectRow_87878(){
something;
}
I put multiple break points (it has more than 20 lines, I put one in for example) in Firebug -> Script tab.
But the problem is, Firebug is not able to do debug these methods, ie. it is not stopping execution it executing as usual. I tried multiple times.
This is my actual code and use:
function onSelectRow_${escapedId }(rowId){
}
<jqgrid:grid onSelectRow="onSelectDeviceRow_${escapedId }"
What can I try to resolve it?
You can use debugger
function onSelectRow_87878()
{
debugger; //add here
something;
}
When you open Firebug, enable Script,and it will automatically go to the debugger point
debugging in firefox
I have been working on a chrome extension and part of my background.js file, it checks to see if the user has used the option page to login.
Here is the code:
chrome.browserAction.onClicked.addListener(function(tab) {
if( localStorage["mainLogin"] == null){
alert("Please go to the option page to login");
}
I know that break; is used in javascript to stop the script. However thats not working on chrome extensions...
Basically, I if this it alert, is should stop and not run the rest of the code.
any help will be greatly appreciated.
break does not stop the code from keep going. It is used to "break" out of a loop. (for/while) However, debugger; can break codes similar to what you want. (This is supported by most of the debugger.)
blah();
debugger; //break point
alert();
If you want to stop a function from keep running, do this:
(function(){
alert("a");
return false;
alert("b"); //you can't see me
})()
For some reason, IE9 is not running my JavaScript code onload when the browser is launched for the first time that session. It seems to only run onload after the user refreshes the page. It will also run the JavaScript when the debug console is open.
How do I make it so the JavaScript runs onload after the browser is open? Is this just a bug of IE9?
I'll restate this so you understand: The code DOESN'T run if you go to the site after launching a new browser session. The code DOES run if you open the site in a new tab, or reload the page, or open the debug console
Here is the function I use to run my script onload (which works fine in NORMAL browsers):
(function (i) {
var u = navigator.userAgent;
var e = /*#cc_on!#*/
false;
var st = setTimeout;
if (/webkit/i.test(u)) {
st(function () {
var dr = document.readyState;
if (dr == "loaded" || dr == "complete") {
i()
} else {
st(arguments.callee, 10);
}
}, 10);
} else if ((/mozilla/i.test(u) && !/(compati)/.test(u)) || (/opera/i.test(u))) {
document.addEventListener("DOMContentLoaded", i, false);
} else if (e) {
(function () {
var t = document.createElement('doc:rdy');
try {
t.doScroll('left');
i();
t = null;
} catch (e) {
st(arguments.callee, 0);
}
})();
} else {
window.onload = i;
}
})(init); //init is the function to call onload
I had the exact same issue that you had. I had a set of images that I wanted to ensure were preloaded before I began starting a slideshow. I was making use of
$(window).load(function(){
//All my code
});
And this is exactly what I was facing.
When I copied and pasted the URL in IE, the onload event did not seem to fire.
If I open the console using F12 and then past the URL in the browser and pressed enter, the everything seemed to be working.
Now that I opened the console at least once,
If I closeed the console and then reloaded the page, the onload was firing.
If I typed the URL and then pressed enter, the onload was firing.
It took me a couple of days to actually figure out what I was doing wrong.
The issue was with the console.log statements. At a lot of places in my code, I had done a lot of console logging. Even one of the plugins that I was using - jplayer has a an uncommented console message somewhere in the code.
The issue was that, unless you open the console at least once in IE, the console object is not available. Which means that the code will fail at the first console.log that it encounters.
Now, I was in no mood to comment out all my console.log statements just for the sake of testing it in IE. So, this is what I did instead. Right at the top of my document.ready jquery function, I wrote this small snippet of code.
if(!window.console){
console={};
console.log = function(){};
}
What it basically does is creates a dummy console.log function placeholder so that the code can run in IE but it will work only as long as console.log is the only console function that you are making use of in your code or in your plugins.
Just my 2 cents. Been pulling my hair over this issue for longer than I care to admit. I hope this is useful to at least someone.
You need to figure out if the code doesn't run at all, I.e. never enters your function, or if it fails on some specific line inside your function. Does IE9 show any warnings or js errors?
The easiest thing to do is stick a bunch of alert() statements in the code to see where it stops and narrow down to that line.
If it never enters your function then you need to look higher, where the call is being made.
Just a small note; When you use any debugging keywords (like console.log) or anything related, IE9 will escape this JS function if and only if the debugger is not on (with F12)
Actually I don't know what else cause a problem, but for me, my problem was the word "console.log" while debugger not on in IE9 ... I know this is already an answered question, but I felt it needs to be be known.
Okay, I figured it out. It has to do with some weird way IE handles IF statements.
In my init function I had two IF statements, one which checked if a variable existed and then logged the value of that variable. The other which checked to see if the value of the same variable was equal to an arbitrary string.
After removing the first IF statement, everything seems to work properly. I also decided to use a different onload function which can be seen below:
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', init, true);
} else if (document.all && !window.opera){ //Crude test for IE
//Define a "blank" external JavaScript tag
document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
var contentloadtag=document.getElementById("contentloadtag");
contentloadtag.onreadystatechange=function(){
if (this.readyState=="complete") {
init();
//ie('open');
}
}
}