Click to See Complete Forum and Search --> : HELP - onClick event to load external


aalsmo
08-06-2003, 08:44 AM
Hi all! I need some help please. What is the javascript for making an image have an onclick event that would load an external .js file into the current document. Please help...

:confused:

SlankenOgen
08-06-2003, 09:18 AM
It depends on what you want to do with the .js file. If it is only for display purposes make a copy of it and give it a .txt extension.

Then, for the image use-

<img src = "menu.gif" onclick = "return A();">

and for the function-

function A(){
document.location.href = "nameOfYourTextFile.txt";
}

aalsmo
08-06-2003, 09:21 AM
See, normally if I wanted it to be onLoad of the page, I would write <language=javascript src="mydoc.js">, but because I want it to basically do a document.write on the click of an image, I'm not sure how to write that code. Does that make sense?

aalsmo
08-06-2003, 09:25 AM
I don't want to actually display the javascript file, I want it to load into the current HTML so it'll do what the javascript file says when they click on the image.

aalsmo
08-06-2003, 09:32 AM
Okay, this is what I have:

function A(){
document.write=('<src="main_popup.js">\n')


<a href="javascript:;"><IMG src="images/homebody_14.gif" alt="" width=259 height=134 border="0" onClick="return A();"></a>


Please help!!!:(

SlankenOgen
08-06-2003, 09:48 AM
This should do it-

<script type = "text/javascript">
var str = "";
str = "<script src = \"mydoc.js\"><\/script>";
document.write(str);
</script>

aalsmo
08-06-2003, 09:57 AM
So are you saying I should do this then:

<script type = "text/javascript">
var str = "";
str = "<script src = \"main_popup.js\"><\/script>";
function A(){
document.write=(str)
}
</script>


AND THEN FOR MY IMAGE TO BE CLICKED:

<a href="javascript:;" onMouseDown="return A();"><IMG src="images/homebody_14.gif" alt="" width=259 height=134 border="0"></a>

SlankenOgen
08-06-2003, 10:30 AM
You cannot use document.write dynamically in this way. Document.write clears the document of all content and then replaces it with the document.write value. In which case your image and the entire contents of the page will be erased to make for the new value.

You can write it as the page loads with just this by itself-

<script type = "text/javascript">
document.write("<script src = \"mydoc.js\"><\/script>")
</script>

This will write what you want as the page loads.

Why are you doing this anyway? It would be easier to just load the js and call functions from it with links.

aalsmo
08-06-2003, 10:34 AM
Okay, what I'm doing, is the .js file has the specs for loading an external flash file transparently over the current html document. Have you ever seen the ones that do it on the page load? Here's an example:

http://www.onlinemedicine.com/

Now, there it loads automatically when the page loads. I don't want to do that. I want it to happen when I click on an image instead. How can I do that?

SlankenOgen
08-06-2003, 10:46 AM
Put the flash movie in a div tag-

<div id ="myFlash">

paste the code for the movie here

</div>

<a href = "#" onclick = "return A();"><img src = "mypic.jpg"></a>

function A(){

document.getElementById("myFlash").style.visibility = "visible";

}

and for the style use-

<style type = "text/css">
#myFlash{visibility: hidden; }
</style>

aalsmo
08-06-2003, 10:58 AM
I feel like such an idiot.....what are you referring to when you said "paste the code for the movie here"....???
Do you mean the entire .js file, or that there is where I should have it load?? Please help...

SlankenOgen
08-06-2003, 11:02 AM
Just copy the code in the flash file from the <object><embed> to </object></embed> tags and paste it into the div tag. You won't need the js file.