The term client side JavaScript refers specifically to JavaScript code that is

The term client side javascript refers specifically

This preview shows page 211 - 213 out of 517 pages.

The term client-side JavaScript refers specifically to JavaScript code that is associated with HTML pages and therefore runs in the browser. Each page in your app that wants to use JavaScript functions or variables must include the necessary JavaScript code itself. The recommended and unobtrusive way to do this is using a script tag referencing the file containing the code, as Figure 6.5 shows. The Rails view helper javascript_include_tag ’application’ , which generates the above tag, can be placed in your app/views/layouts/application.html.haml or other layout template that is part of every page served by your app. If you then place your code in one or more separate .js files in app/assets/javascripts , when you deploy to production Rails will do the following steps automatically: 1. Concatenate the contents of all JavaScript files in this directory; 2. Compress the result by removing whitespace and performing other simple transformations (the uglifier gem); 3. Place the result in a single large file in the public subdirectory that will be served directly by the presentation tier with no Rails intervention; 4. Adjust the URLs emitted by javascript_include_tag so that the user’s browser loads not only your own JavaScript files but also the jQuery library. This automatic behavior, supported by modern production environments including Heroku, is called the asset pipeline . Described more fully in this guide , the asset pipeline also allows us to use languages like CoffeeScript, as we’ll see later. You might think it wasteful for the user’s browser to load a single
Image of page 211
enormous JavaScript file, especially if only a few pages in your app use JavaScript and any given page only uses a small subset of your JavaScript code. But the user’s browser only loads the large file once and then caches it until you redeploy your app with changed .js files. Also, in development mode, the asset pipeline skips the “precompilation” process and just loads each of the JavaScript files separately, since they’re likely to be changing frequently while you’re developing. Minifying is a term used to describe the compression transformations, which reduce the size of the jQuery 1.7.2 library from 247 KiB to 32 KiB. Summary of Client-Side JavaScript and HTML: Like Ruby, JavaScript is interpreted and dynamically typed. The basic object type is a hash with keys that are strings and values of arbitrary type, including other hashes. The fundamental JavaScript data type is an object, which is an unordered collection of property names and values, similar to a hash. Since objects can nest, they can represent hierarchical data structures. JavaScript’s simple object-literal notation is the inspiration for the JSON data interchange format. The preferred unobtrusive way to associate JavaScript with an HTML page is to include in the HTML document’s head element a script tag whose src attribute gives the URL of the script itself, so that the JavaScript code can be kept separate from HTML markup. The Rails helper javascript_include_tag generates the correct URL that takes advantage of Rails’ asset pipeline.
Image of page 212
Image of page 213

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors