Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javascript import

 
Stu Johns
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have 2 javascript files
test1.js and test2.js
I want to call a function in test1.js from a function in test2.js
So i need to do something similar to a java import.
Can this be achieved?

thanks

Stu.
 
Zaeed McColin
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's easy to call a function from another function
for example function a() from 1.js want call function x() from 2.js
OK look
code for a():

and code for x():


so as you see calling methods(functions) not so hard

and about link(import) 1.js and 2.js to document
it's as easy as ABC
just in your head add these guys



 
Stu Johns
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats great, thanks.

Stu
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once both files are included in the page, they all become part of the same scope.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are not sure that a file is added you can do a simple check

if(typeof foo !== "undefined"){ alert("nope"); }

and you can add it in with document.createElement

Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic