What does this do in javascript? - javascript

I came across the function below in one of the SharePoint system javascript files.
function RTE_InsertImage(strBaseElementID)
{ULS1Lu:;
//A bunch of javascript
}
I have never seen something like ULS1Lu:; in any of the javascript code I have worked with before. Does anyone know what this is doing?
Sorry the weak title on the question. I wasn't sure how else to phrase it.

Its a code label, technically they don't need the semicolon, but in javascript it shouldn't hurt. The label will allow a break or continue statement to jump the code back to it.
Most people consider that instead of labels it is better to use function calls whenever possible.

Seems like they are just creating a Label. Possibly, as a marker or for some infamous use. Of course such labels owe a large part of their fame to goto statements.

Related

Split lines to make the code more comprehensible in notepad++

I am trying to modify a js file (It's the first time I try it) and I got something very difficult to understand for me, a mess of inline code with no breaks.
I need it to be clean to be able to understand it, so I've been looking for a solution to break up the text into lines that represents something. I tried to split the lines where the semicolon are, but it didn't solved much.
I remember there where a program that was able to do something similar with css (I can't remember it's name).
Is it possible to do it with notepad++? if not, do you know which program can actually do it?
Thank you
Try using a code beautifier for your javascript, it is currently in 'minified' version.
Example: https://beautifier.io/

How to debug or narrow down logic errors?

I am trying to debug this game I am making. Something is wrong with the for loops and I do not know what it is; essentially I am trying to get my rectangles to spawn on the canvas after the user clicks ready. What are some ways to debug logic errors? Doing this over khanacademy.
When I println the drawRects function. Console says infinite loop and points to my for loops.
When I click ready, the console increases by 1 each time so I know the levelUp function is working.
I cant post another link because not enough rep, but when I println randomRects, nothing appears on the console.
Therefore, I believe it is safe to assume something is wrong with my for loops, because the levelUp function works but the random rectangles are not appearing. What are other debugging techniques I can use to narrow down the problem?
You debug a problem by finding out exactly what the code is doing.
There are a few ways to do that:
Use your head. This is your first line of defense. Run through the code in your head, or better yet with a piece of paper and a pencil. Use some example values for your input, and walk through line by line and see what the code would do. Talk out loud, write stuff down, and do this over and over again. This is a huge part of being a programmer.
Use a console. Processing has a println() function that should go to your JavaScript console in your browser. This is your new best friend. Print out the values of any variables you want to know about, like this:
println("x: " + x);
Or a simple println("here") inside an if statement can tell you whetehr that if statement is executing. Combine this with approach one to really check all of your assumptions.
Use a debugger. If all else fails, step through your code with a debugger. This is basically using a computer to do the first two approaches.
In reality, you'll use a combination of all of the above to debug a problem. But the basic idea is this: you need to understand what your code is doing. You do that by first running through the code in your head or on a piece of paper, and you test any assumptions or anything you aren't sure by printing stuff out.

What's the best practice when modifying twitter bootstrap javascript for your project

In this particular case some of the popover functionality needs to be adapted to our particular needs.
The change itself is quite trivial, it's a modification to this function.
I just need to add another placement, but now I'm not exactly sure how to approach this.
One way would be adding a line to the bootstrap.js file in my repository, that works that has the serious disadvantage of having trouble when updating, I may remember now, but the next one to come after me might generate a hard bug to fix.
Another way I've thought of is extending the function but it might not be easy the way bootstrap is setup. As far as I understand since bootstrap functions are preceded with a plus (+function(){}), they're executed immediately and so I can't get in the way of that.
I've tried modifying this function $.fn.tooltip.prototype.getCalculatedOffset but while it does modify it correctly, the modified one never gets called since it's only called once.
It might be possible but I think the complications of this, outweighs how trivial the fix is (Unless I'm missing a more obvious approach).
Another idea would be forking bootstrap, but I'm not sure what would that accomplish, in the end I'm back with the same problem.
What's the most normal way to approach this? What are the best practices?
Similar to the other answer but using the pop over function, just had to catch the right function!
$.fn.popover.Constructor.prototype.getCalculatedOffse
http://jsfiddle.net/59Er7/3/
A reference is saved to Tooltip as $.fn.tooltip.Constructor in line 1472:
$.fn.tooltip.Constructor = Tooltip
Modifying $.fn.tooltip.Constructor.prototype.getCalculatedOffset (of course not in the original file, but somewhere in your code) should affect the original Tooltip, which is "instantiated" in $.fn.tooltip.
I don't know whether this is best practice, but should work.
jsFiddle Demo

What does JpegMini use to animate the image comparison on the homepage?

Hopefully the title is clear. I'm talking about the large image comparison slider on the homepage of JpegMini.
I've managed to identify it as needing three core files (written as they are named on the site):
jquery.min.js
jquery-ui-1.8.14.min.js
scripts-0135.min.js
It's this last one I'm unable to find any information about so it may hold the secrets but I don't know.
Anyone got any ideas?
The .imagePairs elements have a mousemove event bound:
$('.imagePairs').data('events').mousemove[0].handler
is a function.
The code is apparent inside scripts-0135.min.js, but it is minified.
What you can do is parsing it through jsbeautifier and see if you can make something out of it. It will still have variable names which make no sense, so it will not be easy.
Looking at the code, it seems like they initialize it with $('.imagePairs').myBeforeAfter, which is most probably the function that handles the effect.
I never was able to determine the script used, in the end I searched the web and found a suitable alternative which came with good documentation. http://www.catchmyfame.com/2009/06/25/jquery-beforeafter-plugin/

onclick or onClick?

I thought that binding the click event in javascript is done by using node.onclick, and Chrome/Firefox seem to agree with me, but I saw it written .onClick here 4 times by 3 people, so it can't be a typo and I doubt that it's a coincidence.
So, why are people writing onClick when it does not work?
UPDATE: There are two good explanations; I don't know which one of them is the most plausible, so I will accept the answer based on popular vote, tomorrow.
Because some browsers (depending on the DOCTYPE) are tolerant of the inline onClick="something();" attribute...it seems to have spread a bit, even into JavaScript questions where it doesn't work, since case matters.
Also, specifically to stackoverflow...people using it in questions...well, most of the time they wouldn't be asking a question if their code worked :)
#Nick Craver pretty much has it nailed down and has my vote; I just wanted to add my thought.
I think it's onClick is often used in conversation because it's a bit more readable, and as an old habit from those of us who predate all lowercase HTML. However, in code - both JavaScript and HTML, onclick is correct and the only way it should appear. Even if you're using an older HTML doctype, stick to lowercase. If you ever update to a more strict doctype, you'll be glad your code doesn't need to be checked for case.
It's just that for most browsers HTML attributes are case insensitive, but JS is case-sensitive. onClick will work in html, but if you're defining the handler in JS, you need to use the lowercased onclick.
In that specific question, I used "onClick" as the original question had it in that vein and I try to change as little of the OPs code as possible to make a solution, so that they can see their mistake easily.
The camel case is invalid technically, though I like camel case in general. Frankly, it always annoys me when I see that method, as I think "Where's jQuery!".
It is heavily prevalent in the world, I see it all the time in source.
I edited my answer on the referenced question to fit, thanks for pointing it out.

Categories