Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a javascript jquery function which I need to use twice, in 2 different files.
Now, the question is, which is the better way to implement it, performance-wise. Should I declare function global using 'window' variable, or should I declare the function in each of the files?
Please keep in mind that traffic is not an issue, the script will be used from the hard-drive.
Thanks for your help.
My answer follows the DRY Principle (Don't Repeat Yourself). If you put the function in both files, and later find a bug on Page #1, someone has to remember that the function is duplicated on Page #2 as well (in this case, assume the bug report says "Page #1 doesn't work properly"). From that bug report, would the developer know to also modify Page #2? To avoid the human error piece, I'd always recommend you don't copy/paste functions into multiple locations.
Performance wise, if you're not concerned about traffic, the difference is nanoseconds slower wrapping it in a shared function as you do have to create an additional stack frame for the shared function call that in turn calls jQuery, but we're really talking nanoseconds. For a few nanoseconds lost, I'd say DRY is the way to go.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm currently studying about web development, I still don't know about jquery, but I've a little knowledge about javascript, html and css (basic).
I've been looking at some examples in github to improve my skills, and I've found this content;
https://github.com/stewilondanga/editables
I perfectly understand the theory, but I do not know how to put it into practice, I would like for any similar examples (simplified alternatives) and how to convert the exported code generated by javascript into a html5 table?
Any example would be appreciated! thanks for your attention!
First of all, jQuery does not generate code. It's a framework, you load it into a web page, and then you can use it from within Javascript code in that page.
I suggest you start by looking at the source of https://stewilondanga.github.io/editables/, if an editable tables is what you need. There are more general frameworks to do this, e.g. Aloha
To try it yourself, I'd suggest you bite the bullet equip yourself with some kind of web server, be it on a server somewhere, or on your local machine, so you can easily try out things like this, copy the sources, alter the code etc.., and quickly hit reload on your browser.
While it may seem easier to run a local server and point your browser at http://localhost/something, IMHO it also takes more tinkering to get browsers to embrace that fully. You don't need the extra grief while already learning all those new concepts. If you want to tackle this seriously, consider getting a hosting service or small VPS somewhere. If you don't know how to do that, get help for that first, but get it out of the way. It'll save you much grief.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'll keep my question short. I've read this question about a delay function.
How to wait 5 seconds with jQuery?
and it struck me that people wanted to know about alternative ways to achieve this using JQuery.
In my understanding JQuery is nothing but a JS library. In many cases to introduce new functionality.
Now why would someone use JQuery if an original JS function is available and not even too complex?
Bonus question: Can JS functions be called from within JQuery? (this one, if negative might answer my first one, actually...)
In my understanding JQuery is nothing but a JS library. In many cases to introduce new functionality.
Yes, that is correct. In fact, your astute observation that people want to use jQuery where JS will suffice likely stems from a lack of understanding to this point!
Now why would someone use JQuery if an original JS function is available and not even too complex?
Someone may wish to use jQuery for certain functions if the JS-only equivalent is not well supported in all browsers.
Bonus question: Can JS functions be called from within JQuery? (this one, if negative might answer my first one, actually...)
Certainly! As you stated, jQuery is just a JavaScript library, and can be mixed with bits of plain JS without a problem.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The first time I load the page it's a mess and JavaScript functions don't work. The second time it looks ok and works.
Any brief idea what might be causing this behaviour? I'm using Backbone.js and jQUery Mobile if that makes a difference.
Sorry for no code, but I need to go to a meeting in a moment and the website is huge so I don't really know what part to show you.
If anybody knows why this is happening and doesn't need to see the code, please help.
UPDATE:
Here's what seems to be the answer: https://forum.jquery.com/topic/script-not-running-unless-i-refresh-page
Thank you for suggestions, I upvoted the ones that seem relevant.
It sounds like unmet dependencies: some of your functions probably run before the document is fully loaded and can't get their job done correctly (missing elements, etc..). The second time the function work beacuse the page is cached and so loaed in time before the function starts.
I'd start looking at the JavaScript console and post the errors here if you can't figure it out alone, but without proper informations what we could do is just poor absumptions.
Might be javascript source could not be fetched from the url given. You can do inspect element and see what's happening on console or network tab.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
This title is giving me a warning about the question being subjective but I hope that's not the case.
Here's my situation: I have several functions(some are used on many pages, some only on one page) and I put them all into one .js file to save on load time. However, some pages run functions onload. For this to work, I need to have the functions file declared prior to the call. However, some of the functions require the page to be generated before it can grab the information it needs, so the file declare has to be at the end of the page. So right now, I've made two file declarations on the same page, for the same file. Looking in the console, this obviously causes problems as the first file can't get the information it needs, and throws an error.
So my question: Would it be best to break the functions in to two files (one pre-load one post?) or should the problem ones (which are functions unique to the page anyway) be hard coded at the top?
The best solution would be to break it into two files. That way you can reuse the code in more pages since the pre-load scripts work as they're supposed to, and the post-load scripts get the information they require. It would make the rest of you coding much easier.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a JS script (widget) which is added to other websites. Technically its similar to Google analytics. My question is: what are your advices for ensuring robustness, keeping the code from interfering with other code on the site, avoiding errors for users etc. In general, what should I know to write a professional grade widget.
Notes:
I can't use any JS library such as jquery etc..
I am a big fan of Peter Michaux's guide on how he writes javascript widgets
Also useful are Christian Heilmann's script configuration and the module pattern
Those are generic javascript articles and aren't specific to a single library
Other useful tricks are things like wrapping your code in an anonymous function to stop it interfering with other global libraries.
(function() {
//Your code goes in here
})();
Regarding errors and best practice, John Resig has an interesting article on javascript strict that isn't in yet, but does have some handy information on the sort of things you should be avoiding.
If you're still coming to terms with scoping within your objects, then you might find this article on private and public variables useful as well a a bit more technical definition by Douglas Crockford
Finally, remember to run your completed code through a code quality tool