...

View Full Version : [REQ] Close window onclick and go to link



Camme
06-10-2004, 10:12 PM
I need a script that can do the following :

A pop-up needs to be launched automaticlly.
If you click in the pop-up, the window should close but you should also be redirected to a given link. And last but not least, it has to have a cookie so when the pop-up came up once, it doesn't come up anymore when the user is browsing the site.

I hope you can help me
Thanks in advance
Greetings

homerUK
06-10-2004, 10:18 PM
This should do the trick for you....

insert this code to the page where you want the popup to appear



<script>


function openpopup(){
popup = window.open("popup.htm","","height=250,width=250,scrollbars=yes");
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function loadornot(){
if (get_cookie('poppedup')==''){
openpopup()
document.cookie="poppedup=yes"
}
}

loadornot()
</script>


that will set a cookie so that once it has popped up, it wont do so again... unless you close the browser window and open the page again...

as for the link in the popup, use something like this:




<script language="javascript">
function dolink(url)
{
window.opener.location.href=url;
self.close();
</script>


then your link can be from a button, image or anything... such as

<a href='#' onclick="dolink('mypage.htm')">click me</a>

that will then change the page in the main window and close the popup...

hope that helps!
}

Camme
06-10-2004, 10:29 PM
it doesn't work, sry i'm a javanoob :s
Here's the code :

the pop.html file:


<html>
<head>
<title>daMusic.be : Music is our passion</title>

<script language="javascript">
function dolink(url)
{
window.opener.location.href=url;
self.close();
</script>


</head>

<body topmargin="0" leftmargin="0">


<a href="http://www.damusic.be" target="_blank" onclick="dolink('mypage.htm')"><img

src="{template_url}/pop.jpg" border="0"></a>

</body>
</html>


The header.html file :



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="{direction}">
<head>
<title>{site_name}</title>
<meta http-equiv="content-type" content="text/html; charset={charset}" />
<link rel="stylesheet" href="{template_url}/style.css" />
<script language="javascript" type="text/javascript">
<!--
function opendetailwindow() {
window.open('','detailwindow','toolbar=no,scrollbars=yes,resizable=no,width=680,height=480');
}

function right(e) {
if (navigator.appName == 'Netscape' && (e.which == 2 || e.which == 3)) {
alert(" Copyright by {site_name}");
return false;
}
else if (navigator.appName == 'Microsoft Internet Explorer' && (event.button==2 || event.button == 3)) {
alert(" Copyright by {site_name}");
return false;
}
return true;
}
document.onmousedown=right;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
window.onmousedown=right;
// -->
</script>

<script>


function openpopup(){
popup = window.open("pop.html","","height=300,width=400,scrollbars=no,toolbars=no");
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function loadornot(){
if (get_cookie('poppedup')==''){
openpopup()
document.cookie="poppedup=yes"
}
}

loadornot()
</script>



</head>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>

homerUK
06-10-2004, 10:36 PM
no problems! We all have to start somewhere!! :D

the image which you've linked in the popup should be changed to something like this:

<img src="{template_url}/pop.jpg" border="0" onclick="dolink('http://www.damusic.be')">

assuming that "http://www.damusic.be" is the URL you want to appear in the main page before the popup closes?

Camme
06-10-2004, 10:39 PM
Weird, the popup doesn't even appear and i don't have a popupkiller :s

homerUK
06-10-2004, 10:41 PM
try closing the browser and opening the page again - I just copied the code you posted and it worked fine..... :confused:

Camme
06-10-2004, 10:45 PM
ok, the popup works now, but the image isn't displayed :mad:

*throws his keyboard through the window*

homerUK
06-10-2004, 10:48 PM
oh gosh! Maybe post the code again as you have it now?

Camme
06-10-2004, 10:49 PM
For pop.html


<html>
<head>
<title>daMusic.be : Music is our passion</title>

<script language="javascript">
function dolink(url)
{
window.opener.location.href=url;
self.close();
</script>


</head>

<body topmargin="0" leftmargin="0">


<img src="{template_url}/pop.jpg" border="0" onclick="dolink('http://www.damusic.be')">

</body>
</html>


And i didn't change anything in the header.html file

The pop.jpg file is in the right folder so ...

homerUK
06-10-2004, 10:53 PM
where is the {template_url} coming from? maybe thats not being sent to the popup window or something?

try

<img src="{template_url}/pop.jpg"> on its own and see if that shows. If not then I suspect the template_url isnt working in the popup...?

Camme
06-10-2004, 10:55 PM
dammit, how am i supposed to work it out ?
Maybe just give the link manually ?

llizard
06-11-2004, 12:01 AM
I don't know about the cookie part but this should work for the rest of it:

On the popped up image page:


[...]
<script language="javascript" type="text/javascript">
w=window;
function dolink(url) { self.close();
w.open('','main');
w.focus();
}
</script>
</head>
<body>
<img src="{template_url}/pop.jpg" border="0"
onclick="dolink('http://www.damusic.be');"
width="xx" height="yy" alt="pop"
title="click on image to close window">
[...]


Make sure that the parent window is named "main". Coding for main window:



[...]
<script language="javascript" type="text/javascript">
function dolink(url) { var w=window;
w.name='main';
w.open(url.href,'popup','scrollbars=1,resizable=1,width=100,height=100');
return false;
}
</script>
</head>
<body>

<a href="imagepoptest.html" onclick="return dolink(this);">pop image</a>
[...]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum