Forum Moderators: open
Looking at the code above, you can see that when someone clicks on the image, a "File Download" box comes up asking them if they would like to open or save that document, right?
But here's what I am trying to do...
When the user first clicks on that image, a new window opens. When they click on it again, the "File Download" box comes up so they can either open or save the document.
Looking at some similar postings here, I got the following code, but it only does half the task. It only opens a new window. I couldn't figure out how to get that "File Download" box to come up when they click on the image the second time.
<html>
<head>
<title></title>
<script type="text/javascript">
<!--
var check="true";
function openW() {
if(check=="true") {
window.open("http://www.abc.com/something.html");
check="false";
}
}
// -->
</script>
</head>
<body>
<img src="images/cat.jpg" width="15" height="25" border="0" onclick="openW()">
</body>
</html>
If I do something like this:
<a href="download/cat.doc"><img src="images/cat.jpg" width="15" height="25" border="0" onclick="openW()"></a>
Then the "File Download" box and the new window will come up simultaneously. But I only want the new window to come up on the first click, then the "File Download" box on the second click.
I greatly appreciate any suggestions or insights. Thanks.
. It's<script type="text/javascript">
<!--function openW([blue]link[/blue])
{
if(!link.[blue]clickedOnce[/blue]) {
window.open("http://www.abc.com/something.html");
img.clickedOnce=1;
return false;
}
return true;
}
// -->
</script>
</head><body>
<a href="download/cat.doc" onclick="[blue]return[/blue] openW([blue]this[/blue])">
<img src="images/cat.jpg" width="15" height="25" border="0" >
</a>
-----------------------------------
- Moved the onclick to the link.[i]FIRST CLICK[/i]
A ref to the link is passed to the function.
It is tested for a custom property, [code]clickedOnce, so the!condition is OK, and window is opened.undefined
The property is added to the link (with a nominal value, 1)
The function returns false. This means that the href of the link will not be followed.SECOND CLICK
The property,, now has aclickedOncevalue, so the function just returnstrue.true
The link href is now followed.This setup doesn't need a global variable, so you can use it many times on the same page.
[/code]
One more question for you if you don't mind. What if I have a couple of images:
<a href="download/cat1.doc"><img src="images/cat1.jpg" width="15" height="25" border="0"></a>
<a href="download/cat2.doc"><img src="images/cat2.jpg" width="15" height="25" border="0"></a>
<a href="download/cat3.doc"><img src="images/cat3.jpg" width="15" height="25" border="0"></a>
It doesn't matter which one people will click on first. The first time they click on any of them, a new window comes up. After that, when they click on any of the images again the respective "File Download" box opens. So the new window only comes up once per browser session.
Does this have to do with cookies? Or is it possible to do?
I appreciate your help greatly. Thanks.
openW.active = true;
function openW()
{
if(openW.active){
window.open("http://www.example.com/something.html");
openW.active = false;
return false;
}
else
return true;
}
Then put this in all the links
(not using
[blue]this[/blue] anymore) onclick="return openW();"