Forum Moderators: open

Message Too Old, No Replies

Please help me with this...

regarding "File Download" box and new window

         

H2O_aa

7:38 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



<a href="download/cat.doc"><img src="images/cat.jpg" width="15" height="25" border="0"></a>

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.

Bernard Marx

10:47 pm on Feb 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't tested this one, but give it a go..

<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

. It's
undefined
, so the!condition is OK, and window is opened.
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,

clickedOnce
, now has a
true
value, so the function just returns
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]

H2O_aa

12:32 am on Feb 10, 2005 (gmt 0)

10+ Year Member



Thanks Bernard Marx. But your code opens the new window and the "File Download" box simultaneously. Also, you can only see a blink of the new window as it's being pushed to the background when the "File Download" box opens.

Any way to change that? Thank You.

Bernard Marx

3:24 pm on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, sorry. Slight mistake.

If you look closely, you'll find a 'leftover' in the function.

Change:

[red]img[/red].clickedOnce=1;

To:

[blue]link[/blue].clickedOnce=1;

Bernard Marx

3:28 pm on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



..better still, you could switch to this simpler function,
which appears to work just as well:

function openW(link)
{
window.open("http://www.example.com/something.html");
link.onclick=null;
return false;
}

H2O_aa

4:40 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



Thank you so much Bernard Marx! That did the trick.

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.

Bernard Marx

5:33 pm on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cookies not required for this. Just a 'common' property.
I created a property of the function itself, to keep things clean.

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();"

H2O_aa

6:52 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



Thank you so so much, Bernard Marx. That works perfectly!

I am a beginner in javascripts, currently reading a Learn Javascript for Dummies book. I hope that I would have your knowledge one day.