# How not to include the same javascript files more than once?

**URL:** <https://www.ruby-forum.com/t/how-not-to-include-the-same-javascript-files-more-than-once/190063>\
**Category:** Rails\
**Created:** [June 12, 2010, 12:49pm UTC](https://www.ruby-forum.com/t/how-not-to-include-the-same-javascript-files-more-than-once/190063 "2010-06-12T12:49:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [June 12, 2010, 12:49pm UTC](https://www.ruby-forum.com/t/how-not-to-include-the-same-javascript-files-more-than-once/190063/1 "2010-06-12T12:49:55Z")

</div>

In Ruby on Rails, if a partial (such as \_msgbox.html.erb) need to use  
Javascript by

```
javascript_include_tag :defaults

```

but then, the page layout or other view may also have that same line, so  
the same javascript files will be included multiple time?

Is there a way to tell Rails just to include it at most once? kind of  
like the PHP ways of require\_once or include\_once

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [June 12, 2010, 6:31pm UTC](https://www.ruby-forum.com/t/how-not-to-include-the-same-javascript-files-more-than-once/190063/2 "2010-06-12T18:31:10Z")

</div>

Jian L. wrote:

> In Ruby on Rails, if a partial (such as \_msgbox.html.erb) need to use  
> Javascript by
> 
> ```
> javascript_include_tag :defaults
> 
> ```
> 
> but then, the page layout or other view may also have that same line, so  
> the same javascript files will be included multiple time?
> 
> Is there a way to tell Rails just to include it at most once? kind of  
> like the PHP ways of require\_once or include\_once

You needn’t worry about this. The browser’s caching mechanism should  
ensure that the file is only fetched once.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
