Is it bad practice to include javascript files in Joomla articles? [closed] - javascript

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
I was going to wait until the StackExchange for Joomla was approved before asking this question, but I'd really like to know the answer now!
According to http://docs.joomla.org/Adding_JavaScript there are 3 methods for incorporating javascript into Joomla using-
JDocument::addScriptDeclaration, JDocument::addScript and script.
I believe there is a fourth way which is perhaps easier than the methods listed and that is to include the javascript into the article directly. For example, in a Joomla article I could use the following line <script src="/javascript/jquery-1.7.min.js"></script>
Since this method isn't listed in the docs, I'm thinking it must be bad practice.
QUESTIONs
Is it bad practice to include javascript files directly into Joomla
via an article? If so, why?
What reasons (if any) are there for me to choose the first 3 methods
as opposed to the 4th? (I'm particularly interested to know if the 4th
method would affect cache or cause problems when using cdns.)

Including JS in an article is a bad idea because of loading order and dependencies.
Only do that if you need the script only on that single article page and if you've tested its behavior (and if you are no coder).
Best way to do it is via:
JHtml::_('script', 'path_to_script/script.js', false, true);
If you want to embed it right and not fiddle with PHP you could use something like this:
http://extensions.joomla.org/extensions/core-enhancements/performance/scripts/20245

If you need the script only on that one page, then it's better to add it in the article itself.
You can't use JDocument::addScriptDeclaration, JDocument::addScript because these are PHP functions. Unless your editor allows you to add PHP, then you can't use them.
The 4th option is the best.

The true answer lies in what you are trying to do with your script. There are many uses for Java script in articles, such as syndicated features or affiliate marketing.
TinyMCE limits some of the things you can use your articles for so I use JCE Content Editor for more power and options. There are free versions as well as pro versions available.
Check it out here: http://www.joomlacontenteditor.net/

Related

Can it be a good idea to build a website out of one line of HTML and fully Javascript? [closed]

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 2 years ago.
Improve this question
So what I did was that I created two files index.html and script.js and using all I did on the html was I just used the script tag.
<script src="script.js"></script> and on the script.js file I just typed document.write("<html><h...the whole html in a line></html>). Ok it may not seem like the best idea but it seemed more faster and the styling worked fine, so is it actually a good idea? and mainly is it faster?
No, I can't think of any logical way how that would be faster for a static page. I'm not sure how you were expecting it to work, but instead of just parsing the HTML, it has to run your script, inject the content into the page, and then parse the resulting HTML anyway.
Keep the JavaScript for when you need dynamic content.
My main concern, with constructing a page in the manner you've described is that it may not get indexed into the search engines as well as a page that has static HTML. The web crawler of the search engine would need to be sophisticated enough to run your javascript before scraping the page's text content. I'm not sure they all do that currently, but they should in my opinion.
So, I wouldn't do this on a page you want to be found via the search engines.
There are top-notch web applications built entirely out of JavaScript, using frontend frameworks like React.js, Angular.js, Vue.js, etc.
So it isn't actually a bad idea building your site fully out of JavaScript!
I don't know if you can compare HTML and Javascript frameworks which can be used to build websites (React, Angular, etc.), because they work very differently. Remember, HTML is a markup language, while things such as React and Angular are frameworks written in the JavaScript language.
So to answer your question, is it a good idea to build a website using only JavaScript? - The simple answer is it depends. Using React to make applications or even static websites can save you quite a bit of time, though using plain JavaScript really wouldn't have too many benefits as far as I can see. But if you just want to play around with whatever you're doing, then I'd say sure, go ahead!

Similar and simplified examples (newbie questions) [closed]

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.

Web Scraping - What's a robust and extensible approach? [closed]

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
I have some limited experience with web scraping using tools like Beautiful Soup and Nokogiri.
My approach thus far when looking for information is to first inspect the HTML elements and CSS tags, then applying the selector. While this works, slight differences/changes among web sites would render the code useless. Also, there have been situations where sites simply don't add the selector tags to their HTML elements, so I once had to resort to the hacky approach of selecting the style property of the element.
How would one devise a scraper that would work across multiple sites? I'm aware that the solution would depend on the context, but is there a general good practice in doing it? I was actually asked in an interview before this question and I had no idea.
I have tried googling but much of what I found doesn't go past the basics, and I don't know where to look. Any help would be appreciated.
It's not clear from your question what exactly you are trying to accomplish. If you want the content of the page (like in an article) - you should try goose, which should give you a leg up. You can also try searching for conventional web page approaches like meta tags.
Either way, you should remember that this is the World Wild Web, and the HTML is a very forgiving language, which lets people design pages which are very hard to read by a machine. Even big sites sometimes have their proprietary breaks from conventions, which forces exceptions in your code in order to read them. Site logic may also conflict with conventional logic, or other major site.
This means that your code would probably consist of a lot of use-cases and exceptions.
My suggestion to you is to keep samples of pages of sites you want to scrape, and have a unit test which iterates over them and verifies the scraping results. This way, each time you find a new quirk, you can add it to your collection, and be certain that if the change you made broke some other site's scraping, you would know about it.

Is there a guide about how to create beautiful HTML that is preped to be used with javascript? [closed]

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 might seem like an odd question, but I find that javascript is either easy or hard, depending on how you've coded the HTML. Is there a book or website that goes into detail about successful patterns and guidelines for coding HTML, so that it's very workable with jQuery, css and complex ajax applications? Like solid rules to live by.
Again, seems like a weird question maybe, but I don't know a better way to ask it. I just find myself always having to change the markup as new things come up - like switching between a hidden input element to a data attribute... or putting more ids or taking away ids - and I guess I arrive at the right way to do it, but I'm curious if someone has bothered to analyze this and came up with some great guidelines, standards and patterns so that the resultant HTML is right the first time.
Thanks
The first thing if you want to code some clean HTML that will be easy to work with is to make sure that your code is valid against an official DTD, HTML4 (here) or XHTML (here).
Then use id and class in a proper way (id only for unique section and class for repeatable ones) and name them correctly according to the context so they are easily reachable.
From my experience, I would actually suggest that, when it comes to large projects and professional JavaScript coding, the goal actually becomes to decouple the JavaScript code from whatever HTML it lives in.
As mentioned already, as long as you are using well formed HTML (DTD compliant), a library like jQuery shouldn't have any trouble operating on it. However, as best practice, I would recommend striving to isolate and encapsulate dependencies, whether they be because of HTML structure or just other chunks of JavaScript code.
the best way is to develop the html and javascript together. That way you can adjust the document structure to whatever you need.
This article seems to answer my question:
http://www.viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution/

Best practices for writing javascript widgets [closed]

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

Categories

Resources