What is VanillaJS?
This is VanillaJS (unmodified):
// VanillaJS v1.0
// Released into the Public Domain
// Your code goes here:
As you can see, it's not really a framework or a library. It's just a running gag for framework-loving bosses or people who think you NEED to use a JS framework. It means you just use whatever your (for you own sake: non-legacy) browser gives you (using Vanilla JS when working with legacy browsers is a bad idea).
What does vanilla mean?
Computer software, and sometimes also other computing-related systems
like computer hardware or algorithms, is called Vanilla when not
customized from its original form, meaning that it is used without any
customizations or updates applied to it.
http://en.wikipedia.org/wiki/Vanilla_software
Using strcat in C
Change
char *message = "\n\nHTTP/1.1 ";
to
char message[1024];
strcpy(message,"\n\nHTTP/1.1 ");
and you should be ok, up to a total message length of 1023.
Edit: (as per mjy's comment). Using strcat in this fashion is a great way of getting buffer overflows. You could readily write a small function that checks the size of the buffer and length of incoming string addition to overcome this, or use realloc on a dynamic buffer. IMO, the onus is on the programmer to check correct buffer sizes where they are used, as with sprintfs and other C strings functions. I assume that C is being used over C++ for performance reasons, and hence STL is not an option.
Edit: As per request from Filip's comment, a simple strcat implementation based on a fixed size char buffer:
char buffer[MAXSIZE] = "";
int mystrcat(char *addition)
{
if (strlen(buffer) + strlen(addition) + sizeof(char) >= MaxSize)
return(FAILED);
strcat(buffer,addition);
return(OK);
}
Using dynamic allocation:
char *buffer = NULL;
int mystrcat(char *addition)
{
buffer = realloc(buffer, strlen(buffer) + strlen(addition) + sizeof(char));
if (!buffer)
return(FAIL);
strcat(buffer, addition);
return(OK);
}
In this case you have to free your buffer manually when you are finished with it. (Handled by destructors in C++ equivalents)
Addendum (Pax):
Okay, since you didn't actually explain why you had to create message[1024]
, here it is.
With char *x = "hello", the actual bytes ('h','e','l','l','o',0) (null on the end) are stored in an area of memory separate from the variables (and quite possibly read-only) and the variable x is set to point to it. After the null, there's probably something else very important. So you can't append to that at all.
With char x[1024]; strcpy(x,"hello");
, you first allocate 1K om memory which is totally dedicated to x. Then you copy "hello" into it, and still leave quite a bit of space at the end for appending more strings. You won't get into trouble until you append more than the 1K-odd allowed.
End addendum (Pax):
When to use Vanilla JavaScript vs. jQuery?
this.id
(as you know)this.value
(on most input types. only issues I know are IE when a<select>
doesn't havevalue
properties set on its<option>
elements, or radio inputs in Safari.)this.className
to get or set an entire "class" propertythis.selectedIndex
against a<select>
to get the selected indexthis.options
against a<select>
to get a list of<option>
elementsthis.text
against an<option>
to get its text contentthis.rows
against a<table>
to get a collection of<tr>
elementsthis.cells
against a<tr>
to get its cells (td & th)this.parentNode
to get a direct parentthis.checked
to get the checked state of acheckbox
Thanks @Tim Downthis.selected
to get the selected state of anoption
Thanks @Tim Downthis.disabled
to get the disabled state of aninput
Thanks @Tim Downthis.readOnly
to get the readOnly state of aninput
Thanks @Tim Downthis.href
against an<a>
element to get itshref
this.hostname
against an<a>
element to get the domain of itshref
this.pathname
against an<a>
element to get the path of itshref
this.search
against an<a>
element to get the querystring of itshref
this.src
against an element where it is valid to have asrc
...I think you get the idea.
There will be times when performance is crucial. Like if you're performing something in a loop many times over, you may want to ditch jQuery.
In general you can replace:
$(el).attr('someName');
with:
Above was poorly worded. getAttribute
is not a replacement, but it does retrieve the value of an attribute sent from the server, and its corresponding setAttribute
will set it. Necessary in some cases.
The sentences below sort of covered it. See this answer for a better treatment.
el.getAttribute('someName');
...in order to access an attribute directly. Note that attributes are not the same as properties (though they mirror each other sometimes). Of course there's setAttribute
too.
Say you had a situation where received a page where you need to unwrap all tags of a certain type. It is short and easy with jQuery:
$('span').unwrap(); // unwrap all span elements
But if there are many, you may want to do a little native DOM API:
var spans = document.getElementsByTagName('span');
while( spans[0] ) {
var parent = spans[0].parentNode;
while( spans[0].firstChild ) {
parent.insertBefore( spans[0].firstChild, spans[0]);
}
parent.removeChild( spans[0] );
}
This code is pretty short, it performs better than the jQuery version, and can easily be made into a reusable function in your personal library.
It may seem like I have an infinite loop with the outer while
because of while(spans[0])
, but because we're dealing with a "live list" it gets updated when we do the parent.removeChild(span[0]);
. This is a pretty nifty feature that we miss out on when working with an Array (or Array-like object) instead.
Is plain vanilla JavaScript better than using frameworks like jQuery or MooTools?
Frameworks solve cross-browser bugs which normally would cost hours of your time, so you can focus on functionality instead of worrying about some edge case browser bug.. instead of wasting 4-5 hours solving a bug spend that time with your family.
Frameworks such as jQuery are pretty loaded with stuff like animation, selectors, html manipulation so there's usually some sort of functionality already built into the library, again saving you more time and the API makes it really easy to actually accomplish complex things.
Interpreters and browsers are only getting faster and faster so I don't particularly think it's a huge issue loading an entire library up. In addition thanks to Google et al we get very fast cdns and nowadays lots of sites are using the same exact URI to pull the script in, meaning there's a higher rate of the script getting cached and reused on another site.
Instead of every single web developer having their own library it's much more efficient having thousands of people concentrated to bettering a handful of libraries so cross-browser bugs get documented and fixed.
Competition is a good thing, the result of the slickspeed tests resulted in much faster selector engines such as Sizzle. Developers not having to worry about trivial DOM bugs means more complex libraries are created daily, which means entry-level developers have access to very powerful plugins.
As far as security, jQuery for example will detect if the browser is capable of parsing JSON natively and if so, rely on that. Usually any modern browser will have this, and it's much safer than eval
... so jQuery strives to use the safer and more secure methods first. It will only use eval if there isnt a JSON.parse method available.
An important thing to remember in jQuery though is remembering you're still coding in Javascript. Usually people get too caught up in the sugar coated methods and wrapping everything in $
, I think it's important to know you can still do this.href
instead of $(this).attr('href')
if you would like an absolutely normalized uri for example.
Why is Vanilla JS not running my if statement?
I've fixed the program.
For future reference and others', here's how I did it.
I made the
num
var globalvar num;
getRandImg(){//bla bla bla}
Separated get randImg() and checkAns()
Made a Boolean to check if it is the first time laoding, so I dont check for the next answer as @Barmar said.
You seem to be randomizing the question after the user has already answered. Is that what it's supposed to do? –
Barmar
Thanks for all of your help stackoverflow people! I've also made a repo for the project, if you wanna see the full code. https://github.com/elfryt/whats-the-interchange
Related Topics
Short-Polling VS Long-Polling for Real Time Web Applications
How to Declare a Global Variable in JavaScript
Prototype: Deep Scope of "This" to Access Instance's Scope
Remove HTML Tags in JavaScript with Regex
When to Use Jsx.Element VS Reactnode VS Reactelement
How to Add Text Inside the Doughnut Chart Using Chart.Js
How to Understand Usecapture Parameter in Addeventlistener
Firestore: What's the Pattern for Adding New Data in Web V9
Handle Url Fragment Identifier (Anchor) Change Event in JavaScript
How to Use JavaScript or Jquery to Read a Pixel of an Image When User Clicks It
How to Check Whether <Ng-Content> Is Empty? (In Angular 2+ Till Now)
Jquery Function to Get All Unique Elements from an Array
Firefox Error Rendering an Svg Image to HTML5 Canvas with Drawimage
(Deep) Copying an Array Using Jquery
How to Round a Number in JavaScript? .Tofixed() Returns a String
How to Merge Two JavaScript Objects Together in Es6+