Google Answers Logo
View Question
 
Q: The “onclick” event does not occur when an external javascript file is used. ( No Answer,   3 Comments )
Question  
Subject: The “onclick” event does not occur when an external javascript file is used.
Category: Computers > Programming
Asked by: ftppro-ga
List Price: $20.00
Posted: 29 Dec 2005 11:28 PST
Expires: 29 Dec 2005 20:37 PST
Question ID: 610968
I need to display HTML from an external javascript file. However, the
?onclick? event does not occur when a <div> tag contains text that is
retrieved from an external javascript file.

To exemplify this problem, please create the following four small files:

==============================================
-------------------------------
1. good.htm:
-------------------------------
<html><head><title>Good</title></head><body>
<script>function DoClick(objDiv){alert('Click is OK.')}</script>
<div onclick=DoClick(this)><script SRC=test2.js></script></div>
</body></html>
-------------------------------
2. bad.htm:
-------------------------------
<html><head><title>Test</title></head><body>
<script src="test1.js"></script>
</body></html>
-------------------------------
3. test1.js:
-------------------------------
document.write("<script>function DoClick(objDiv) {alert('Click is
OK.')}</script> <div onclick=DoClick(this)>From test1.js.</div><br>
<div onclick=DoClick(this)> <script SRC=test2.js>  </script></div>")
-------------------------------
4. test2.js:
-------------------------------
document.write("From test2.js.")
-------------------------------
==============================================

The following instructions will demonstrate this problem (use IE 6.0):

1. Display good.htm, and click on ?From test2.js?. An alert window
will indicate that ?Click is OK?.

2. Display bad.htm, and click on ?From test1.js?. An alert window will
indicate that ?Click is OK?.

3. Also from bad.htm, click on ?From test2.js?. The alert window is
not displayed. Therefore, the click event is not occurring.

To solve this problem, the click event must occur in bad.htm, when
?From test2.js? is clicked (after test2.js is displayed from
test1.js).

Request for Question Clarification by sublime1-ga on 29 Dec 2005 12:53 PST
ftppro...

Given the source you've provided for bad.htm, neither
"From test1.js" nor "From test2.js" is displayed to 
be clickable, as you've indicated it should be.

sublime1-ga

Clarification of Question by ftppro-ga on 29 Dec 2005 14:30 PST
Thank you all for your prompt attention to my question. I am willing
to offer an extra $25 to the person who can find a SOLUTION (and not
just an explanation why it can?t be done).

Here are my replies to the responses that you provided:

1. For sublime1: After you remove the line feeds from test1.js, please
confirm that bad.htm works as described.

2. For cascaval: I need a solution which does not require modifying bad.htm.

3. For winista: I need a solution for IE, not Firefox.

4. For everyone: This code will be used to monitor ad clicks. Here are
the requirements:

a. In bad.htm: There can only be one SCRIPT tag, which will eventually
load a javascript file (test1.js) from my server.

In test1.js:
b. In the final version, the DoClick function will contain a reference
to an image file, which will call a script on my server, which will
log each click in my database. For this example, the DoClick function
only needs to display an alert message.

c. Within the DIV tag,  the script tag will load a javascript file
from an Ad Network (e.g. Fastclick, Kanoodle, etc.), which will cause
an ad to be displayed on the webpage.

Please provide a modified test1.js which fulfills the requirements described above.

Thanks!

Request for Question Clarification by sublime1-ga on 29 Dec 2005 18:36 PST
ftppro...

Removing the linefeeds did the trick. Thanks.

Clarification of Question by ftppro-ga on 29 Dec 2005 19:03 PST
Sublime1:

I hope you can find a solution this issue.

Thanks.
Answer  
There is no answer at this time.

Comments  
Subject: Re: The “onclick” event does not occur when an external javascript file is used.
From: cascaval-ga on 29 Dec 2005 13:18 PST
 
The reason why the second DIV form test1.js is not clickable is that
the output of test2.js is not written inside the DIV. It means that
the DIV is actually empty because the outputed text "From test2.js" is
not inside.

One would have to know exactly what you are trying to achieve to give
you a nice workaround so I can just give you this demonstration:

http://www.cascaval.com/ga/610968/bad.htm
Subject: Re: The “onclick” event does not occur when an external javascript file is used.
From: winista-ga on 29 Dec 2005 13:23 PST
 
I think this is happening because of how document.write functionality
works in javascript. If you have any document.write call inside a
script tag, the output of the write is rendered after closing script
tag. Since you have a document.write inside another one, so the output
of second one is comping after close of first one.
Subject: Re: The “onclick” event does not occur when an external javascript file is used.
From: winista-ga on 29 Dec 2005 13:24 PST
 
Forgot to mention.... your implementation does work in FireFox. It
seems that those folks got this right and IE has bug in this area.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy