Hello!
I have found a very strange thing with firefox handling of window.location.href. It sort of works but not really.
This is what I have done:
[code]
function linkRewriter(obj)
{
var linkUrl = “test.php”;
var linkText = obj.href;
// see if local
var current = window.location.href;
var place = current.indexOf(‘/’, 7);
if ( place != -1 )
current = current.substr(0, place );
if ( linkText.indexOf(current) ) { // only rewrite external links
linkText = encodeURIComponent(obj.href);
var newText = linkUrl + “?url=” + linkText;
// obj.href = newText; // this will pretty much break stuff, but works mostly
window.location.href = newText;
// window.location.replace(“http://www.google.com”);
}
return true;
}
Then, in the page, I use it with something like:
[code]
<a href=”http://www.google.se” onclick=”linkRewriter(this);”>the link</a>
On the server I use php like this:
[code]
$trueUrl = urldecode($_GET[‘url’]);
$dbUrl = $trueUrl;
$dbUrl = strtr($trueUrl, ‘”‘\’, ‘___’);
// db config
$server = “127.0.0.1”;
$username = “something”;
$passwd = “something”;
$db = “something”;
$table = “something”;
$col = “link”;
if ( $_GET[‘url’] ) {
$link = new mysqli($server, $username, $passwd, $db);
if ( ! $link->connect_error ) {
$query = “insert into ” . $table .” set ” . $col . ” = “” . $dbUrl . “””;
$link->query($query);
}
$link->close();
// send the user to the site
header(“HTTP/1.1 302 Moved Temporarily”);
header(“Location: ” . $trueUrl);
exit();
}
Now to the strange stuff, this *sort of*cant
If I try to use some other stuff in window.location.href firefox just ignores it, and sends the browser to what is in the href attribute, for example:
[code]
<head>
<script type=”text/javascript”>
function strange()
{
window.location.href = “http://www.google.com”;
return true;
}
</script>
</head>
<body>
<a href=”http://www.sunet.se” onclick=”strange();”>google</a>
</body>
will send me to [url]www.sunet.se[url]www.google.com
I just dont get it. What is happening?
Is this some sort of strange “security feature”, but why then is
[code]
obj.href = “http://www.google.com”;
instead of the window.location stuff working?
(and, no, using obj.href will not work in my redirect after saving to db code, since if you go back the url is *really
/erik