• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Call methods of external javascript file?

 
Ranch Hand
Posts: 33
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a javascript file which creates a button using innerHTML
like


div = document.createElement('div');
div.innerHTML="<input type='button' value='Login' />";

this is demo.js


Now I want to call a method say hello() which is in some other javascript file say hello.js

e.g,

var objHello = {

sayHello: function() {
alert("Hello World");
}
};

I have to run this demo.js using GreaseMonkey4IE on IE,
Right now it successfully creates the button but the problem is to call sayHello() method of hello.js (because it is an external file).
In html we can simply use <Script> tag using src="jsName" but I have no idea how to do it here.


Please help
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would have to append that JavaScript file to the page to use it.

Eric
 
Mohit J Kumar
Ranch Hand
Posts: 33
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric,
Thanks for the reply.
Right now I am doing exactly what you are saying.
I was wondering if I could manage multiple JS files.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All script files imported into the page are loaded into the same scope. Just be sure that any code that runs inline has what needs loaded before it executes.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

james kurk wrote:Hi Eric,
Thanks for the reply.
Right now I am doing exactly what you are saying.
I was wondering if I could manage multiple JS files.



What does the code look like? Maybe there is a flaw in your code?

Eric
 
Mohit J Kumar
Ranch Hand
Posts: 33
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//================MyJs.js=========================

var objGreeting = {
Greetings : function() {
toolbardiv = document.createElement('toolbardiv'); // create div tag dynamically
toolbardiv.setAttribute('id',"toolbar"); // give id to it
toolbardiv.className="top"; // set the style classname

//set the inner styling of the div tag
toolbardiv.style.position="absolute";

//set the html content inside the div tag
toolbardiv.innerHTML="<input id='Greeting-Button' type='greet' value='Login' onClick='objHello.SayHello()'/>"
}
},

var objHello = {

SayHello: function() {
alert("Hello World");
}
};

Right now this all is in a single JS file and it's working fine.

Now I want to separate the two classes above into two different js files.
But after doing this I am not able to call the SayHello method.

I am using "GreaseMonkey for IE" and IE8 to run this script.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic