• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Order of script tags

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am facing a problem..
I have used two script tags in my HTML page..fortunately or unfortunately they both contain a function with the same name, when i try to invoke that vary function which of the function will be called... any idea??
Consider this scenario...
*****HTML FIle*****
<html>
<script language="JavaScript" src="first.js"></script>
<script language="JavaScript" src="second.js"></script>
<BODY>
<input type="button" value ="order" on Click="order()"/>
</BODY>
</HTML>
*******First.js*********
function order() {
alert("first");
}
*******Second.js*********
function order() {
alert("first");
}
When I tried this i got the alert message "second".. is there any funda/logic for that??
 
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 can not have two functions named the same thing.
A javascript included like that is like having it typed on the page.
Eric
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how can i avoid a situation when i m importing more then one Javascript file...
One of them is my module related ... other one is common file for the application ....and on some bad day some one adds a function in the common javascript with the same name as present in my javascript file...
ANy idea??
[ November 24, 2003: Message edited by: Andy Smith ]
 
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
That is why all functions should have unique names so you do not run into this problem.
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all thanx Eric for quikies...
But any trick to avoid this???
as i m not aware when some one can add the method in the common javascrip files..with the same name as i hav in mine...

and i alwayz want the method of my file to be executed...
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that the last function that is compiled by the page will be called. The last function compiled here is second.js' order() function.
Interestingly, javascript does not overload methods as well. A javascript function can be passed any number of arguments when it is invoked.
hope this helps...
Thanks
Maneesh
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Maneesh,
Even i thought like this, I have tested this on IE, will this work on Navigator too??
Is this result somewhere documented or we are just trying to look the behaviuor by result only..??
I just need to confirm this behaviour..
Thx Maneesh & Eric..
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Problem Andy.
The onUnload Method should work with netscape too... however, I have only tried it with IE and it works with no problem. If you find another solution, do let me know...
Thanks,
Maneesh
 
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

Originally posted by Andy Smith:
First of all thanx Eric for quikies...
But any trick to avoid this???
as i m not aware when some one can add the method in the common javascrip files..with the same name as i hav in mine...

and i alwayz want the method of my file to be executed...


Name them orginal so No one else would use them! use your name with the function name etc...
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its very strange and shocking to know that JavaScript is too weak to handle this thing.. anyway thax u all for ur responses...
Cheers
 
Sheriff
Posts: 67694
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

Its very strange and shocking to know that JavaScript is too weak to handle this thing..


No language allows ambiguous references. You should either follow Eric;s recommendation to use unique names, or use classes to separate your methods into unique namespaces.
bear
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Bear .and Eric ....
but i think atleast javascript should have thrown some kind of error in this case..
ANyway Will follow ur suggestion .. and if i get something new on this i will get bac.. to this topic
 
reply
    Bookmark Topic Watch Topic
  • New Topic