/    Sign up×
Community /Pin to ProfileBookmark

firefox location.href again…

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;
}
[/code]

Then, in the page, I use it with something like:

[code]
<a href=”http://www.google.se” onclick=”linkRewriter(this);”>the link</a>
[/code]

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();
}
[/code]

Now to the strange stuff, this *sort of* works. The sites are added to the db, but I *cant* redirect to any other site than the site in the href attribute of the a tag, or to be more correct, firefox pretty much ignores what I redirect to or what I do on the php page, it takes me to the page in the href attribute of the a tag. Strange!! Very strange!!! It is as if the php page was never run except for the fact that things are added to the db.

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>
[/code]

will send me to [url]www.sunet.se[/url] and NOT [url]www.google.com[/url]! Strange!

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”;
[/code]

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* rewritten and calling linkRewrite with the rewritten code will turn the url into something that is wrong)

/erik

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 07.2008 — &lt;script type="text/javascript"&gt;
function strange()
{
window.location.href = "http://www.google.com";
return [COLOR="Blue"]false[/COLOR];
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a href="http://www.sunet.se" onclick="[COLOR="Blue"]return[/COLOR] strange();"&gt;google&lt;/a&gt;
Copy linkTweet thisAlerts:
@egrcauthorSep 07.2008 — Thanks!

I found the error myself yesterday. First I returned false from the function when testing it, but it didn't work (because of the missing return in onclick). Then, as a last resort, I tested with both returning true and false and couldn't understand why setting the location didn't work, while setting the obj.href did. After finding the error and adding the return to the onclick everything worked as expected with both location.href and obj.href. A simple error that cost me some hours.

There is one strange part left though. When I was leaving out the return from onclick, changing the location.href in the function got stuff saved in the db!!!!

That is, firefox did, in some way, send the request to the php script responsible for saving the data, even though firefox didn't render the php page and instead loaded the page as specified by the href attribute of the a tag. Thus, when I left out the return from the onclick changing the location.href made firefox send a request to the page specified in the changed location.href, but it didnt render that page, but, as expected, loaded the page in the href attribute. This is strange! It seems to be a bug???

/erik
Copy linkTweet thisAlerts:
@FangSep 08.2008 — When the server script returns from saving the the data, the href has already fired opening a new window, leaving the server script with nowhere to go.
Copy linkTweet thisAlerts:
@egrcauthorSep 08.2008 — When the server script returns from saving the the data, the href has already fired opening a new window, leaving the server script with nowhere to go.[/QUOTE]

Yes, but should it really be this way?? Changing location.href sends the request to the new page no matter what you return from the function in firefox, but it only loads the new page in the browser if the function is returning false (and you have a return in onclick).

For example, in Opera, the request is only sent when the function returns false (and there is a return in the onclick). The same goes for safari. Konqueror loads the page no matter if the returned value is true or false and there is no need for "return" in onclick. This doesnt say much since browsers differ, but Opera, Safari and Konqueror are at least consistent - either you send the request and load the page or you dont. Firefox always seems to send the request, but then, as you say, dumps the response.

Ok, this is not a big matter, but in this case I prefer the way Opera, Safari and Konqueror handles it.

/erik
×

Success!

Help @egrc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...