...

View Full Version : Is there such a script ?



paz
10-09-2008, 05:26 PM
Hi,

I have been searching your scripts to solve a particular problem that I have but have not found the solution. Indeed it may not be possible.

The Problem :confused:
I have a site with a number of static pages and on those pages are links to txt and pdf documents. Currently when a visitor clicks the link it starts the download as usual. Instead I would prefer that when the user clicks the link to the document they are shown a confirmation screen with a possible advert or message that once they OK then downloads the link.

Is this possible using a script?

Thanks in advance. :thumbsup:

jmrker
10-09-2008, 07:34 PM
Take a look at the JS 'confirm' statement.
See: http://www.exforsys.com/tutorials/javascript/javascript-confirm-box.html

paz
10-13-2008, 11:55 AM
This is almost what I need. Except that I need to be able to display HTML in the dialogue box.

paz
11-06-2008, 11:58 AM
I am bumping this again. Still really need an answer to this if possible.

Philip M
11-06-2008, 12:10 PM
This is almost what I need. Except that I need to be able to display HTML in the dialogue box.

You can't. You would have to create your own modal dialogue box for this.

You could of course display a different pre-defined message in the confirm box depending on which link was clicked.

4B/75/73/41/
11-06-2008, 09:14 PM
So you want HTML tags to be visible in a pop up message box?

paintingtheweb
11-06-2008, 09:33 PM
This should point you in the right direction...

<html>
<script type="text/javascript">
function confirm_download(element){
file = element.href;
//display popup with confirm button calling the download_file() function
//make sure to pass the filename along
popup(file);
}
function popup(file){
//display popup
download_file(file);
}
function download_file(file){
window.location = file;
}
</script>
<a href="document.pdf" onclick="confirm_download(this)" >Click Here</a>
</html>

paz
11-11-2008, 02:44 PM
Many many thanks 'paintingtheweb'. This pointed me towards the following code which is perfect for my application and excatly what I was after.

Script
<SCRIPT LANGUAGE="JavaScript">
function NewWindow() {
win2=window.open("http://www.yourwebsite.com/popup.html","NewWindow","toolbar=no,directories=no,menubar=no,scrollbars=no,width=320,height=280");
}
</SCRIPT>


Calling it
onclick='NewWindow();'>

This allowed me to pop up a page/popup/message for every PDF clicked on. Excellent. :thumbsup::thumbsup::thumbsup::D

paz
11-11-2008, 07:49 PM
I have an additional question for you based on this script.

Is there a way to universally call a NewWindow() without having to add the calling code to every download link on the page.

In other words, tell the page to always display a NewWindow() whenever a user clicks on ANY *.pdf link or any *.exe link. This would prevent a lot of additional code on pages that contain a large quantity of pdf and exe links.

If there was it would be very nice, any ideas ? :rolleyes:

itsallkizza
11-11-2008, 08:36 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Example</title>
<script type="text/javascript">
// <![CDATA[

function changeEmUp()
{
var all_anchors = document.getElementsByTagName("a");
var set_href = function(a_element,to)
{
if (a_element.setAttribute) a_element.setAttribute("href",to);
else a_element.href = to;
}
var get_href = function(a_element)
{
return a_element.getAttribute ? a_element.getAttribute("href") : a_element.href || false;
}
for (var i=0;i<all_anchors.length;i++)
{
if(get_href(all_anchors[i])) set_href(all_anchors[i],"javascript:NewWindow('"+get_href(all_anchors[i])+"')");
}
}

// ]]>
</script>
</head>
<body onload="changeEmUp()">

<a href="http://www.google.com">Google</a><br />
<a href="http://www.yahoo.com">Yahoo</a>

</body>
</html>


Actually this function is a little better, just because it follows the href=# onclick=function pattern:


function changeEmUp()
{
var all_anchors = document.getElementsByTagName("a");
var set_href = function(a_element,to)
{
if (a_element.setAttribute) a_element.setAttribute("href",to);
else a_element.href = to;
}
var get_href = function(a_element)
{
return a_element.getAttribute ? a_element.getAttribute("href") : a_element.href || false;
}
for (var i=0;i<all_anchors.length;i++)
{
if (get_href(all_anchors[i]))
{
all_anchors[i].goto = get_href(all_anchors[i]);
set_href(all_anchors[i],"#");
all_anchors[i].onclick = function()
{
NewWindow(this.goto);
return false;
}
}
}
}

paz
11-12-2008, 01:11 PM
This is great stuff, I appreciate this and am learning a lot here.

itsallkizza, I have used the second function and it works well. The only problem is that it workes no matter what I click on. If I only wanted this to be called when I click on a PDF or EXE or some other file link. Could this be done ?

Thanks again for all the great advice. :thumbsup:

itsallkizza
11-13-2008, 05:03 PM
function changeEmUp()
{
var all_anchors = document.getElementsByTagName("a");
var set_href = function(a_element,to)
{
if (a_element.setAttribute) a_element.setAttribute("href",to);
else a_element.href = to;
}
var get_href = function(a_element)
{
return a_element.getAttribute ? a_element.getAttribute("href") : a_element.href || false;
}
for (var i=0;i<all_anchors.length;i++)
{
if (get_href(all_anchors[i]))
{
all_anchors[i].goto = get_href(all_anchors[i]);
if (all_anchors[i].goto.indexOf("exe") != -1 || all_anchors[i].goto.indexOf("pdf") != -1)
{
set_href(all_anchors[i],"#");
all_anchors[i].onclick = function()
{
NewWindow(this.goto);
return false;
}
}
}
}
}

paz
11-14-2008, 04:54 PM
Thank you thank you thank you.

This little bit of code has now transformed my approach to the links on my site and also made me think differently about my own code.

Cheers :thumbsup:
Paul.

paz
11-21-2008, 03:31 PM
itsallkizza,

This code does not seem to work. It loads the popup when I click to download a ZIP or PDF, however the download does not start. Can you check this for me.

Thanks
Paul.

itsallkizza
11-21-2008, 05:18 PM
Thanks for your email. Always feel free to ask me questions via e-mail, but I'm going to respond here on the forum so anyone else who has a similar problem can see what we're up to :)

If I understand your needs correctly, then you should create a server-side intermediary something like this...
confirm_download.php:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Confirm Download</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[
function back()
{
if (history && history.go) history.go(-1);
return false;
}
// ]]>
</script>
</head>
<body>

<?php
$furl = $_GET["furl"] ? $_GET["furl"] : null;
if ($furl)
{
?>

<!-- You can put as much html here as you want and style it normally with CSS to make a pretty confirmation page. -->
<h2>Are you sure you want to download <?php echo($furl) ?>?</h2>
<a href="<?php echo($furl) ?>">Yes</a> <a href="#" onclick="return back()">No</a>

<?php
}
else echo("Sorry, you have been misdirected to this page.");
?>

</body>
</html>


Then change your NewWindow function:


function NewWindow(file_url)
{
win2 = window.open("confirm_download.php?furl="+file_url,"NewWindow","toolbar=no,directories=no,menubar=no,scrollbars=no,width=320,height=280");
}


And you should be good to go :)

paz
11-21-2008, 05:28 PM
Am I correct in thinking that the server side PHP replaces the original code. Is the code added to the page in question ?

I have another problem in that the server that this is on does not have PHP.

I am sorry for my massive lack of knowledge on this, I am a complete noob. :rolleyes:



p.s. Could the script just be modified to also re-request the download ?

itsallkizza
11-21-2008, 07:04 PM
Am I correct in thinking that the server side PHP replaces the original code. Is the code added to the page in question?

No, it's an intermediary page that will be displayed as a popup (the download confirmation you wanted).

You could do this in JS alone but it's not ideal. What sort of server-side code is available for use on your server? ASP, JSP, Ruby? PHP is free so maybe you can just install that on your server?

paz
11-21-2008, 07:23 PM
Here is my current code


<html>
<head>
<title>Popup Auto Linker</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="Test Popup Page" />
<meta name="keywords" content="" />

<SCRIPT LANGUAGE="JavaScript">
function PopUp1() {
win2=window.open("http://www.mywebsite.com/popup1.html","PopUp1","toolbar=no,directories=no,menubar=no,scrollbars=no,width=320,height=330");
}
function autolinkpopup()
{
var all_anchors = document.getElementsByTagName("a");
var set_href = function(a_element,to)
{
if (a_element.setAttribute) a_element.setAttribute("href",to);
else a_element.href = to;
}
var get_href = function(a_element)
{
return a_element.getAttribute ? a_element.getAttribute("href") : a_element.href || false;
}
for (var i=0;i<all_anchors.length;i++)
{
if (get_href(all_anchors[i]))
{
all_anchors[i].goto = get_href(all_anchors[i]);
if (all_anchors[i].goto.indexOf("zip") != -1 || all_anchors[i].goto.indexOf("bin") != -1)
{
set_href(all_anchors[i],"#");
all_anchors[i].onclick = function()
{
PopUp1(this.goto);
return false;
}
}
}
}
}
</SCRIPT>

</head>
<body bgcolor="#000000" link="#99FF33" vlink="ffffffffffffff" onLoad="autolinkpopup();">

<div align="center">
<p><font color="#FFFFFF"><a href="http://www.google.com">Link to Google</a></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><a href="test.zip">test.zip</a></font></p>
</div>
</body>
</html>

Can this script simply be modified to work. All I need to do is just download the ZIP as well.

Thanks.

paz
11-28-2008, 12:19 PM
Bump for itsallkizza :thumbsup:

itsallkizza
12-02-2008, 04:32 PM
Sorry bout the wait. I lose track of threads sometimes :)

I set up an example for you using the code I gave in the previous posts. Actually download the test.zip for both files.

http://buildyourownbagel.com/test/test_main.html

paz
12-03-2008, 05:56 PM
Hi,

I have worked on your excellent scripts and they work fine. I now think the problem is that my ISP is not running PHP on the server that my domain is hosted on. When I look at your source code for the PHP (from your test server link) I can see that the URL has been passed correctly. When I look at the source code for my PHP on my site I can still see the furl command and not the URL which is why it fails to find the test.zip when you click YES to download.

Is there any way that I can check the status of the server hosting my domain, or

Is there any other way that I can acheive the same result without using PHP ?

itsallkizza
12-03-2008, 08:39 PM
You can do it all via Javascript.

Here, I coded this up for ya :) (example: http://buildyourownbagel.com/test/test_main-js.html):


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<link rel="stylesheet" type="text/css" href="css/" />
<script type="text/javascript" src="js/"></script>
<style type="text/css">
.confirm_box
{
position: absolute;
background-color: #ffffff;
border: 1px solid black;
font-family; arial,sans-serif;
color: #ff0000;
font-size: 12px;
text-align: center;
padding: 10px;
}

.confirm_box a
{
margin: 0 6px;
}
</style>
<script type="text/javascript">
// <![CDATA[
function changeEmUp()
{
var all_anchors = document.getElementsByTagName("a");
var set_href = function(a_element,to)
{
if (a_element.setAttribute) a_element.setAttribute("href",to);
else a_element.href = to;
}
var get_href = function(a_element)
{
return a_element.getAttribute ? a_element.getAttribute("href") : a_element.href || false;
}
for (var i=0;i<all_anchors.length;i++)
{
if (get_href(all_anchors[i]))
{
all_anchors[i].goto = get_href(all_anchors[i]);
if (all_anchors[i].goto.indexOf("exe") != -1 || all_anchors[i].goto.indexOf("pdf") != -1 || all_anchors[i].goto.indexOf("zip") != -1)
{
set_href(all_anchors[i],"#");
all_anchors[i].onclick = function()
{
NewWindow(this);
return false;
}
}
}
}
}

function getXYOfElement(elm)
{
var x = 0;
var y = 0;
if (elm.offsetParent)
{
do
{
x += elm.offsetLeft;
y += elm.offsetTop;
}
while (elm = elm.offsetParent);
return [x,y];
}
return false;
}

function getWidthHeightOfElement(elm)
{
if (elm.offsetWidth && elm.offsetHeight) return [elm.offsetWidth,elm.offsetHeight];
return false;
}

function set_href(a_element,to)
{
if (a_element.setAttribute) a_element.setAttribute("href",to);
else a_element.href = to;
}

function NewWindow(anchor_element)
{
var body = document.getElementsByTagName("body")[0];
if (document.createElement && body.appendChild)
{
var f_url = anchor_element.goto;
var confirm_box = document.createElement("div");
confirm_box.className = "confirm_box";
confirm_box.innerHTML = 'Are you sure you want to download '+f_url+'?<br />';

var a1 = document.createElement("a");
set_href(a1,f_url);
a1.innerHTML = "Yes";
confirm_box.appendChild(a1);

var a2 = document.createElement("a");
set_href(a2,"#");
a2.parent_box = confirm_box;
a2.onclick = function()
{
document.getElementsByTagName("body")[0].removeChild(this.parent_box);
return false;
}
a2.innerHTML = "No";
confirm_box.appendChild(a2);

var left_pad = 10;
var xy_of_anchor = getXYOfElement(anchor_element);
confirm_box.style.left = (xy_of_anchor[0]+left_pad+getWidthHeightOfElement(anchor_element)[0])+"px";
confirm_box.style.top = xy_of_anchor[1]+"px";
body.appendChild(confirm_box);
}
}
// ]]>
</script>
</head>
<body onload="changeEmUp()">

<a href="http://www.google.com">Google.com</a><br />
<a href="test.zip">test.zip</a>

</body>
</html>

paz
12-04-2008, 12:21 PM
Morning, that is a cool peice of code. Is there any way that the confirm box can be a seperate popup window that can also contain HTML. The point of doing this is to display a small HTML advert along with the confirmation buttons.

itsallkizza
12-04-2008, 04:16 PM
There is always a way :)

Example: http://buildyourownbagel.com/test/test_main-js_2.html

main page:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[
function changeEmUp()
{
var all_anchors = document.getElementsByTagName("a");
var set_href = function(a_element,to)
{
if (a_element.setAttribute) a_element.setAttribute("href",to);
else a_element.href = to;
}
var get_href = function(a_element)
{
return a_element.getAttribute ? a_element.getAttribute("href") : a_element.href || false;
}
for (var i=0;i<all_anchors.length;i++)
{
if (get_href(all_anchors[i]))
{
all_anchors[i].goto = get_href(all_anchors[i]);
if (all_anchors[i].goto.indexOf("exe") != -1 || all_anchors[i].goto.indexOf("pdf") != -1 || all_anchors[i].goto.indexOf("zip") != -1)
{
set_href(all_anchors[i],"#");
all_anchors[i].onclick = function()
{
NewWindow(this.goto);
return false;
}
}
}
}
}

var win2;
function NewWindow(file_url)
{
win2 = window.open("confirm_download.html?furl="+file_url,"NewWindow","toolbar=no,directories=no,menubar=no,scrollbars=no,width=320,height=280");
}
// ]]>
</script>
</head>
<body onload="changeEmUp()">

<a href="http://www.google.com">Google.com</a><br />
<a href="test_2.zip">test_2.zip</a>

</body>
</html>


confirm page:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Confirm Download</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[

function pullURLVars()
{
var vars_arr = new Array();
var page_url = document.location.href || window.location || document.location;
page_url = page_url.split("?");
if (page_url[1])
{
var vstrs = page_url[1].split("&");
for (var i=0;i<vstrs.length;i++) vars_arr.push(vstrs[i].split("="));
}
return vars_arr;
}

function grabEntryWhere(two_d_arr,look_for,opt_loc)
{
for (var i=0;i<two_d_arr.length;i++)
{
if (opt_loc != undefined)
{
if (two_d_arr[i][opt_loc] == look_for) return two_d_arr[i];
}
else
{
for (var n=0;n<two_d_arr[i].length;n++)
{
if (two_d_arr[i][n] == look_for) return two_d_arr[i];
}
}
}
return false;
}

function set_href(a_element,to)
{
if (a_element.setAttribute) a_element.setAttribute("href",to);
else a_element.href = to;
}

function closePage()
{
if (window.close) window.close();
}

function setup_confirm()
{
var varr = pullURLVars();
var furl = grabEntryWhere(varr,"furl",0)[1];
document.getElementById("file_name").innerHTML = furl;
set_href(document.getElementById("give_me_href"),furl);
}

// ]]>
</script>
</head>
<body onload="setup_confirm()">

<!-- You can put as much html here as you want and style it normally with CSS to make a pretty confirmation page. -->
<h2>Are you sure you want to download <span id="file_name"></span></h2>
<a id="give_me_href" href="">Yes</a> <a href="#" onclick="closePage()">No</a>

</body>
</html>

paz
12-11-2008, 11:08 AM
That code works very well indeed. I have implemented it on the site and its great.

Could this code also be modified to simply popup a html screen with no confirm or other interaction. Just a simple popup.

itsallkizza
12-11-2008, 03:38 PM
Ya, just clear out all the unwanted html in the confirm page.

paz
12-11-2008, 06:16 PM
OK, your code is working perfectly on any page with basic content and just a few (10-20) links on it. However, when I use it on a large page that has a lot of images and a lot of links (300) it does not seem to work. I am guessing that it has something to do with the onload() command having to wait until the page is loaded and the page being large.

On these large pages, if I refresh the page your code starts to work.

Is there any way to ensure that the code works every time ?

itsallkizza
12-11-2008, 11:29 PM
Send me an example link, I'll take a look.

It sounds like the script is hanging up due to too many lines being run - if that's the case I can work on speeding it up a bit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum