473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

External JS Call Problem??

384 256MB
I'm having an issue with calling a function from a external linked JS file. The file is linked like so in the header of the page:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" src="file.js"></script>
And the function that exists in the file.js is called from the body of the html page like so but its not working???
Expand|Select|Wrap|Line Numbers
  1. <script>myfunction('test');</script>
  2.  
Apr 20 '09 #1
20 3308
Dormilich
8,658 Expert Mod 8TB
try
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">myfunction('test');</script>
if that fails too, have a look at the error console.
Apr 20 '09 #2
ziycon
384 256MB
That didn't work, the error console is saying:
myfunction is not defined
But it is in the JS file!?
Apr 20 '09 #3
Dormilich
8,658 Expert Mod 8TB
did you check writing (JS is case sensitive, so myfunction() and myFunction() are different)?
Apr 20 '09 #4
ziycon
384 256MB
Yup, checked this and its all ok, its a stange one as its the way i've always called functions from an external JS file!?!
Apr 20 '09 #5
Dormilich
8,658 Expert Mod 8TB
can you post a link to that file?
Apr 20 '09 #6
ziycon
384 256MB
This is all thats in the file:
Expand|Select|Wrap|Line Numbers
  1. function myfunction(input) {
  2.     document.write(input);
  3. }
Apr 20 '09 #7
Dormilich
8,658 Expert Mod 8TB
are you sure the file is loaded correctly?
Apr 20 '09 #8
dmjpro
2,476 2GB
@Dormilich
Ahh! That's the point ;)
Actually what i think that when he calls the "function" then the JS file not loaded.
You better to keep your "this type of function(what you call inside HTML body)" in your script tag.

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. //some function definitions
  3. </script>
  4. ....
  5. ....
  6. ....
  7. ....
  8. <script>/*call the function*/</script>
  9. ....
  10. ....
  11.  
Apr 20 '09 #9
Dormilich
8,658 Expert Mod 8TB
@dmjpro
this sure solves the problem, but you pay it with bandwidth traffic and download time (this is no problem when having only some very small functions, but if you need the benefits of larger libraries, this is not an option)

some background:
external JS files are treated similar to images, if the browser has this file cached (i.e. in its memory), it will only look if the server provides a newer file and only in that case the file is loaded again (relevant when you need a script on many pages)
Apr 20 '09 #10
dmjpro
2,476 2GB
Then first time it will not work.
And how can i make sure when i make this call then my external file gets loaded completely? ;)
Apr 20 '09 #11
Dormilich
8,658 Expert Mod 8TB
@dmjpro
if you have access to the HTTP request (e.g. in FireBug*) you'll see a 404 status for that file. (200 for all OK, 304 for file not changed)
@dmjpro
a simple test is
Expand|Select|Wrap|Line Numbers
  1. if (myfunction) alert("myfunction() exists");
* Firefox WebDev extension
Apr 20 '09 #12
dmjpro
2,476 2GB
@Dormilich
The code must be like this ..

Expand|Select|Wrap|Line Numbers
  1. if(typeof myfunction!= 'undefined') //some code
  2.  
Yeah i thought of this..If i need to call that "function" at that moment then ?
Apr 20 '09 #13
Dormilich
8,658 Expert Mod 8TB
@dmjpro
throw an Exception* and quit. but usually there's no problem in including an external file**.

* seen at MDC
Expand|Select|Wrap|Line Numbers
  1. throw new TypeError();
** especially if you manage to separate JS code from HTML. some handy functions for this are provided through the event handlers. further you can replace document.write() calls by DOM manipulation (createElement(), appendChild(), …).
Apr 20 '09 #14
Markus
6,050 Expert 4TB
Wouldn't

Expand|Select|Wrap|Line Numbers
  1. window.onload = function ()
  2. {
  3.     myfunction();
  4. }
  5.  
overcome that problem too?

Or calling it from <body onload="myfunction()">?
Apr 20 '09 #15
Dormilich
8,658 Expert Mod 8TB
@Markus
I (personally) don't consider that as an option.

although the original problem is (I think) rather, whether the external file is loaded or not. otherwise I can't imagine what cause should be there for the error message.
Apr 20 '09 #16
dmjpro
2,476 2GB
@Markus
See "onLoad" event fired when page loaded completely. Now before getting loaded fully page needs to do something which will be by JavaScript that's where the context comes in ;)
What i saw in GMail ... basically the Mail ID box gets focus first then at "onLoad" the Password box gets focus.
Apr 20 '09 #17
Markus
6,050 Expert 4TB
@dmjpro
Sorry, what?
Apr 20 '09 #18
Dormilich
8,658 Expert Mod 8TB
@dmjpro
what advantage should that have? the password box is the first item to be used, I don't see a benefit for marking the ID box first (besides that you can mimic that with a little timeout).
Apr 20 '09 #19
dmjpro
2,476 2GB
@Markus
What you posted .. "ziycon" needs to execute some JS code before the page getting loaded fully but you suggested to execute this after the page loaded fully ;)
Apr 20 '09 #20
dmjpro
2,476 2GB
@Dormilich
Actually what i meant here .... before page loading fully Mail ID box gets focus and after page loaded fully the Password box gets focus. That's what i said .. Actually i am not against this policy, it should be .. Actually What "Markus" said, with respect to that post i gave an example.
May be some confusion created over here ;) sorry!
Apr 20 '09 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Scott Allen | last post by:
Hello, I'm new to C++ development and I'm trying out figure out the cause of an 'unresolved external symbol' error that I'm receiving when compiling. Here is some history on what I'm doing: I...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
1
by: Chuck Mendell | last post by:
I am having problem with external javascripts. My OS is XP Pro. I am told to create an external javascript using a .js extension. (I did that) The external .js is very simple, containing: ...
9
by: keith | last post by:
I created a class libery which has name space Assembly and class Assembly and compiled it. Then created a C# project and called a method in the external class e.g. Assembly dll;...
6
by: Olivier Lechenne | last post by:
I am converting some projects from VS 6.0 to VS .NET 2003 and I get a linker error error LNK2001: unresolved external symbol ___security_cookie or error LNK2001: unresolved external symbol...
0
by: wingman | last post by:
Hello All I'm completely new to ASP.Net. I need to write a page to fetch an external call which actually call a Java program to download a file, the content of this downloaded is...
3
by: Trapulo | last post by:
I've a datarepeater that loads custom external templates with loadtemplate and bind data to them. All ok. Now I need to localize some text labels, but I don't know how can I change this data. If I...
1
by: Dominic via DotNetMonster.com | last post by:
I am a CA-OpenROAD programmer, but I attempting to build some VB.NET DLLs and access them through OpenROAD as an external Windows TypeLib. I have managed to build a simple DLL (code follows) but...
2
by: PrettySneaky | last post by:
I have bumbed into a frustrating problem with my webservice. When I run he web site in Visual studio 2005 everything works fine. But when i try to publish it, it seems to be unable to call a method...
3
by: lookaround | last post by:
Hi everyone, I need some help... I call an external exe (a command-line tool) with Process.Start through this code: try { p = new Process(); p.StartInfo.UseShellExecute = false;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.