aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Call methods of external javascript file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Call methods of external javascript file?" Watch "Call methods of external javascript file?" New topic
Author

Call methods of external javascript file?

Mohit J Kumar
Ranch Hand

Joined: Apr 08, 2010
Posts: 33

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

"It's good to B in touch...So keep responding"
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You would have to append that JavaScript file to the page to use it.

Eric
Mohit J Kumar
Ranch Hand

Joined: Apr 08, 2010
Posts: 33

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

Joined: Jan 10, 2002
Posts: 61618
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Apr 08, 2010
Posts: 33

//================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.
 
subject: Call methods of external javascript file?