473,748 Members | 7,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function not defined when js in external file

4 New Member
Hi guys,

Here's the code I'm to refer to:

[HTML]
<html>
<body>
....

<form method="get" action="http://www.google.com/search" name="google" />
<input type="text" name="q" size="25" maxlength="255" value="- search this site -" onclick="ClearI fAppropriate(); " class="textbox" />
<a onclick="submit ();" href="#">Search </a>
</form>

...
</html>[/HTML]

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="JavaScript" >
  2. <!--
  3. var LabelText = "- search this site -";
  4.  
  5. if(document.google.q.value.length == 0) {
  6.     document.google.q.value = LabelText;
  7.     }
  8.  
  9. function ClearIfAppropriate() {
  10. if(document.google.q.value == LabelText) {
  11.     document.google.q.value = "";
  12.     }
  13. }
  14. //-->
  15. </script>
Now for the problem - it's related to the onclick="ClearI fAppropriate(); " function. Firefox will execute this function just fine if I embed the js in the html file after the function call; however it fails if I try to do it either of these ways:

* if I embed the code BEFORE the function call, e.g. in the <head> zone where it would normally belong, the code runs properly but FF generates a javascript error 'document.googl e has no properties' and directs me to the line if(document.goo gle.q.value.len gth == 0) of the js code.
* if I put the code in a separate file with <script src="...> anywhere on the page, be it in the header zone or after the function call where I previously embedded the code, the function fails when clicking on the text box (which calls the function) and I get a js error 'ClearIfAppropr iate is not defined' which directs me to the first line of the html code <!DOCTYPE ....

Now I've linked to code before and had no problems, in fact <a onclick="submit ();" href="#"> refers to a function in a linked file
Expand|Select|Wrap|Line Numbers
  1. function submit() {
  2. document.google.submit();
  3. }
and this works fine. Clearly one solution is to embed the code after the function call but this is messy and I don't think it should be necessary. It looks to me as though the issue is related to the code being executed before the external function has been imported or something; though why the same works for the other function is beyond me.

Would appreciate any help you can offer.

Thanks!
Greg
Sep 17 '07 #1
7 10289
gits
5,390 Recognized Expert Moderator Expert
hi ...

welcome to TSDN ...

simply remove the script-tags and the html-comment-tags from the external javascript file ...

kind regards
Sep 17 '07 #2
gregp1
4 New Member
Thanks for that! Sometimes one can't see the wood for the trees.

That restores the normal behaviour of inline compared to linked, but but I still get a 'document.googl e has no properties' error when the function call appears after the function link (e.g. when it's linked in the page header). This doesn't happen for the other function submit();. I can always link it below the search box but I'd prefer to have it in the header. What am I doing wrong?

Thanks again!
Sep 17 '07 #3
acoder
16,027 Recognized Expert Moderator MVP
On line 5, you're checking for the value of document.google .q.value, but it doesn't exist yet. You could make this check on page load if you want.
Sep 17 '07 #4
gregp1
4 New Member
Yes that correlates with the code working if linked after it's called. So how do I implement your suggestion? <body onload="....."> or something? Sorry I'm still pretty green on extending js!
Sep 17 '07 #5
acoder
16,027 Recognized Expert Moderator MVP
Yes, put it inside a function and then call using body onload.
Sep 18 '07 #6
gregp1
4 New Member
Thanks, that worked!

Sometimes experts can assume us newbies know more than we actually do, so for anyone else reading this who is as green as I am, here's what I changed:

javascript:

Original code:
Expand|Select|Wrap|Line Numbers
  1.     if(document.google.q.value.length == 0) {
  2.         document.google.q.value = LabelText;
  3.         }
  4.  
New code:
Expand|Select|Wrap|Line Numbers
  1. function initialise() {
  2.     if(document.google.q.value.length == 0) {
  3.         document.google.q.value = LabelText;
  4.         }
  5. }
  6.  
HTML:
Original HTML:
[HTML]<body>[/HTML]
New HTML
[HTML]<body onload="initial ise();">[/HTML]

Thanks all for your help!
Greg
Sep 19 '07 #7
acoder
16,027 Recognized Expert Moderator MVP
Glad you got it working. Post again anytime should you need help solving another problem. Your explanation was nice and simple for any newbie to understand (hopefully).
Sep 19 '07 #8

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

Similar topics

0
1133
by: mm | last post by:
Hi, Is it true that we can use dynamic SQLs in external user-defined scalar functions but not in table functions? I saw an example of "dynamic SQL in an external scalar function" in a sample file coming with DB2 (the Convert() function in the file sqllib/samples/c/udfemsrv.sqc). I tried to write a table function in C which executes dynamic SQLs to
4
3076
by: Nish | last post by:
Hi, I have a question regarding inline functions in C. If I declare a function as inline in the header file but do not define it there. Then I define that function in .c file, however neither make it static nor inline nor extern in the .c file. What would the compiler (gcc) do in this case? Will it just create object code for that function which can be called from any translation unit and never inlined. Or will it create the object...
26
2151
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null); this.property1 = arg; this.property2 = aConstantDefinedGlobally; this.method1 = function (anArg) {
2
5331
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: 1>make_buildinfo.obj : error LNK2019: unresolved external symbol __imp__RegQueryValueExA@24 referenced in function _make_buildinfo2 Ask on python-list@python.org . - Josiah
3
1955
by: sam_cit | last post by:
Hi Everyone, I have seen in some project where functions are declared as extern, what is the possible reason to do this? To my best understanding, if some other file wan't to invoke this function, it could very well be done ny including a header file which specifies the function prototype...
11
2523
by: Old Wolf | last post by:
Does the following program require a diagnostic? Which section of the Standard deals with this? (I read the section on function calls and didn't see anything). void f(void); int main(void) { f(); } Is the answer still the same if the function declaration was: static void f(void);
6
2618
by: Richard | last post by:
1. Are there any problems with having, for instance, POSIX's "open" function #defined more than once. In my case, these would be in different static libraries: #ifdef __cplusplus extern "C" { #endif #define open myOpenFunction1
14
2329
by: jg | last post by:
Does C++ standard require an inline function be generated all the time ? For example, #include <iostream> using namespace std; inline int foo() {
28
8885
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and didn't find any good explanation about it. Could the experts please give me an explanation or point me to some link? Thanks! /Why Tea
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.