...

View Full Version : redirect with javascript



blommethomas
02-03-2007, 09:51 AM
<HTML>
<HEAD>
<TITLE> Homepage of Thomas Blomme </TITLE>
<META name="Author" lang="en" content="Thomas Blomme">
<META name="keywords" lang="en" content="homepage, java, netbeans, JDance,linux, gentoo, mysql, php">
<META http-equiv="content-type" content="text/html; charset=UTF-8">
<META name="date" content="2007-01-20" >
<meta http-equiv="Content-Script-Type" content="text/javascript">


<SCRIPT language="javascript" src="../functions.js">
</SCRIPT>
</HEAD>

<BODY>
<TABLE height="100%" width="100%">
<TR align="center">

<TD><IMG src='pictures/language_nl.png' align='middle' onclick="createLink('main.php?lang=nl')"></IMG></TD>
<TD><IMG src='pictures/language_en.png' align='middle' onclick="createLink('main.php?lang=en')"></IMG></A></TD>

</TR>

<TR align="center">
<TD height="10%" colspan="2">No javascript? <A href="alt_index.html">yes</A></TD>
</TR>
</TABLE>
</BODY>
</HTML>


I use the code above, to send a link to a javascript function which should make some adjustments to it, add version info, screen size and so on to the url and then redirect to it, at the moment I use the follwing code for that:



var width = screen.width;
var referrer = document.referrer;
var agent = navigator.userAgent;
var version = navigator.appVersion;

function createLink(link)
{
link = createInfo(link);
window.location(link);
}

function createInfo(link)
{
patternMain = /main.php\?/;
patternWidth = /width=/;
patternReferrer = /referrer=/;
patternAgent = /agent=/;
patternVersion = /version=/;
patternMark = /\?$/;
patternMark2 = /&$/;
if(link == "main.php")
{
link = "main.php?width=" + width + "&referrer=" + referrer + "&agent=" +
agent + "&version=" + version;
}
else if(patternMain.test(link))
{
document.write("main");
if(!patternWidth.test(link))
{
if(!patternMark.test(link) && !patternMark2.test(link))
{
link = link + "&";
}
link = link + "width=" + width;
}
if(!patternReferrer.test(link))
{
if(!patternMark.test(link) && !patternMark2.test(link))
{
link = link + "&";
}
link = link + "referrer=" + referrer;
}

if(!patternAgent.test(link))
{
if(!patternMark.test(link) && !patternMark2.test(link))
{
link = link + "&";
}
link = link + "agent=" + agent;
}

if(!patternVersion.test(link))
{
if(!patternMark.test(link) && !patternMark2.test(link))
{
link = link + "&";
}
link = link + "version=" + version;
}
}
return link;
}



this seems however to redirect to the same page as before and not to main.php

I have added a line of code to main.php, which I should see when the url is correctly submitted:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>

<HEAD>
<TITLE>Homepage of Thomas Blomme</TITLE>
<META name="Author" lang="en" content="Thomas Blomme">
<META name="keywords" lang="en" content="homepage, java, netbeans, JDance,linux, gentoo, mysql, php">
<META http-equiv="content-type" content="text/html; charset=UTF-8">
<META name="date" content="2007-01-20" >
</HEAD>

<BODY>
<?php
echo "referrer=$referrer";
?>
</BODY>

</HTML>


to see the results: blommethomas.downfire.com

chump2877
02-03-2007, 10:39 AM
what's the purpose of this line in your JS:

document.write("main");That could cause some issues....I would remove the line...


Edit: And the syntax for window.location is:


window.location="yoururl.com"

blommethomas
02-03-2007, 10:51 AM
thanks, that solved it,

such a stupid syntax error, I hate myself :P



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum