Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Redirection - via Disclaimer Page

Status
Not open for further replies.

JSDNUK

Programmer
Feb 13, 2006
21
GB
I've been having a few problems getting a Javascript Redirection system setup. Basically, what i'm trying to do is modify all my site's external links to send a querystring to a disclaimer page containing the requested URL.

I'm not sure what I've messed up, but whenever I open the page in browser I get a blank page. Could it be the forward slashes or single quotes causing this? Or would it be the script in the header (document.location.href = "disclaimer.html?"url;)... or a combination of the above???

Any help would be most appreciated.

Thanks!!

Code for referrer page...

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Enfield website: content page with link</title>
<script language="javascript" type="text/javascript">
function redirect(url) {
   document.location.href = "disclaimer.html?"url;
}
</script>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
document.write(' <a href="#" onclick="redirect('[URL unfurl="true"]http://www.ninemsn.com.au')">External[/URL] site link JS - Ninemsn</a> ');
//-->
</script>
<noscript>
<a href="[URL unfurl="true"]http://www.google.com"[/URL] target="_blank">External site link - Google</a>
</noscript>
</body>
</html>

Code for disclaimer page (not built yet, but this is the logic)

Code:
1. Save querystring as variable querystring = querystringFromURL
2. Start timer - 5 second delay
3. Insert querystring into javascript redirect: 

document.location.href = querystring;
 
This line:

Code:
document.location.href = "disclaimer.html?"url;

contains invalid syntax, and so will most likely cause a JS error.

You should probably have a "+" sign before the work "url".

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Also, this line will error:

Code:
document.write(' <a href="#" onclick="redirect('[URL unfurl="true"]http://www.ninemsn.com.au')">External[/URL] site link JS - Ninemsn</a> ');

as you need to escape the quotes-within-quotes:

Code:
document.write(' <a href="#" onclick="redirect([!]\[/!]'[URL unfurl="true"]http://www.ninemsn.com.au[/URL][!]\[/!]')">External site link JS - Ninemsn</a> ');

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Dan,

Have this sending the querystring beautifully now!! The querystring reader came together quite easily as well.

Code below should anyone be looking for something similar.

Referrer Page...

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Enfield website: content page with link</title>

<script language="javascript" type="text/javascript">
function redirect(url) {
	alert(url);	
	document.location.href = "disclaimer.html?url=" + url;
}
</script>

</head>
<body>
<script language="javascript" type="text/javascript">
<!--
document.write(' <a href="#" onclick="redirect(\'[URL unfurl="true"]http://www.ninemsn.com.au\')">External[/URL] site link JS - Ninemsn</a> ');
//-->
</script>
<noscript>
<a href="[URL unfurl="true"]http://www.google.com"[/URL] target="_blank">External site link - Google</a>
</noscript>
</body>
</html>

Disclaimer/Redirection Page...

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<!-- <META HTTP-EQUIV="Refresh" CONTENT="5; URL=http://www.ninemsn.com.au?"> -->

<title>Enfield website: disclaimer</title>
<script>
fullURL = parent.document.URL
externalLink = fullURL.substring(fullURL.indexOf('?')+5, fullURL.length)
the_timeout = setTimeout("document.location.href = externalLink", 5000);
</script>

</head>
<body>
<strong>Disclaimer Page</strong>
<br />This is our standard disclaimer page.
</body>
</html>
 
instead of a timer, how would you use a link or button on the page for the user to click on to reach the desired site?
 
Code:
<a href="whatever.html" onclick="document.location.href = '[URL unfurl="true"]http://someurl.com/';[/URL] return(false);">link text</a>

Or, if you wanted to include a reference to the original URL for those with JS disabled:

Code:
<a href="[URL unfurl="true"]http://someurl.com/whatever.html"[/URL] onclick="document.location.href = this.href + '?user=wibble'; return(false);">link text</a>

Hope this helps,

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top