File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call methods of external javascript file?

 
Mohit J Kumar
Ranch Hand
Posts: 33
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64175
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic