HOW TO INCLUDE A JAVASCRIPT FILE IN ANOTHER JAVASCRIPT FILE

In obatambeienwasirherbal.com, khổng lồ use $ instead of jQuery prefix, i added the following code around all js code:

jQuery(function($) ...);It works well, but i cannot call an object from another javascript tệp tin, the following problem persists:

Slider is not definedThis is my code:

file1.js

jQuery(function($) var slider = new Slider();); file2.js

jQuery(function($) function Slider() this.nb_ele = 10; );It seems that because the workspace of two js files is different so they can"t call function from another one. Any solution?


jquery javascript
Share
Improve this question
Follow
edited Mar 28 "16 at 2:49
*

fuxia♦
103k3333 gold badges234234 silver badges426426 bronze badges
asked Mar 27 "16 at 22:58
*

Trong Lam PhanTrong Lam Phan
14311 gold badge11 silver badge55 bronze badges
Add a phản hồi |

2 Answers 2


Active Oldest Votes
4
The issue you have sầu is that Slider is not accessible outside of its scope as that"s defined by the .ready() though you"ve sầu used shorthvà for that. The function needs khổng lồ be available in the global scope:

file1.js

function Slider() this.nb_ele = 10;jQuery(function($) // ...);file2.js

jQuery(function($) var slider = new Slider();); If you want Slider khổng lồ stay in the document ready, you"ll need to use a function expression lớn add the function khổng lồ the global object:

jQuery(function($) window.Slider= function () this.nb_ele = 10; ;);See Also: https://stackoverflow.com/questions/1449435/splitting-up-jquery-functions-across-files


Share
Improve this answer
Follow
edited May 23 "17 at 12:40
*

Community♦
1
answered Mar 28 "16 at 1:38
*

DammeulDammeul
28811 silver badge66 bronze badges
1
Add a phản hồi |
2
I had the same kind of problem.

You can also solve it with:

var $ = jQuery.noConflict();$(document).ready(function()your code in here);You need also khổng lồ use that trick in all of your javascript files, i mean var $ = jQuery.noConflict();.

Bạn đang xem: How to include a javascript file in another javascript file

After that Dammeul is right.


Share
Improve sầu this answer
Follow
edited Mar 28 "16 at 2:06
answered Mar 28 "16 at 1:57
*

LoicTheAztecLoicTheAztec
2,8861111 silver badges1919 bronze badges
0
Add a bình luận |

Your Answer


Thanks for contributing an answer to obatambeienwasirherbal.com Development Stack Exchange!

Please be sure lớn answer the question. Provide details và tóm tắt your research!

But avoid

Asking for help, clarification, or responding to lớn other answers.Making statements based on opinion; baông chồng them up with references or personal experience.

Xem thêm: file_get_contents php

To learn more, see our tips on writing great answers.


Draft saved
Draft discarded

Sign up or log in


Sign up using Google
Sign up using Facebook
Sign up using E-Mail & Password
Submit

Post as a guest


Name
E-Mail Required, but never shown


Post as a guest


Name
Thư điện tử

Required, but never shown


Post Your Answer Disthẻ

By clicking “Post Your Answer”, you agree khổng lồ our terms of service, privacy policy và cookie policy


Not the answer you're looking for? Browse other questions tagged jquery javascript or ask your own question.


The Overflow Blog
Featured on Meta
Related
0
Problem with custom function from jQuery slider tutorial
1
Problem in loading javascript in footer
0
$_POST empty in using new php file added to lớn theme
0
Điện thoại tư vấn current post URL in ajax
Hot Network Questions more hot questions

Question feed
Subscribe lớn RSS
Question feed To subscribe lớn this RSS feed, copy và paste this URL into your RSS reader.


lang-js
obatambeienwasirherbal.com Development
Company
Staông xã Exchange Network
site thiết kế / hình ảnh sản phẩm © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.8.23.40047


obatambeienwasirherbal.com is a trademark of the obatambeienwasirherbal.com Foundation, registered in the US và other countries. This site is not affiliated with the obatambeienwasirherbal.com Foundation in any way.
obatambeienwasirherbal.com Development Stachồng Exchange works best with JavaScript enabled
*

Your privacy

By clicking “Accept all cookies”, you agree Staông xã Exchange can store cookies on your device và discđại bại information in accordance with our Cookie Policy.