Reputation: 33986
I made a bookmark that users can add and it sends them to my site capturing the referrer.
<a href="javascript:location='http://www.chusmix.com/tests/?ref='+escape(location.href);" onclick="alert('Drag it, not click it!');return false;"> Bookmark </a>
My problem is that for some reason the location.href part instead of printing http:// it prints: "http%3A//". I want to remove it and get just the domain.com
I have a similar code that maybe could be useful but I'm having a hard time figuring out how to implement it inside HTML.
// Function to clean url
function cleanURL(url)
{
if(url.match(/http:\/\//))
{
url = url.substring(7);
}
if(url.match(/^www\./))
{
url = url.substring(4);
}
url = "www.chusmix.com/tests/?ref=www." + url;
return url;
}
</script>
Thanks
Upvotes: 1
Views: 167
Reputation: 12727
Like already stated in my comment:
Be aware that this kind of bookmarking may harm users privacy, so please inform them accordingly.
That being said:
First, please use encodeURIComponent()
instead of escape()
, since escape()
is deprecated since ECMAScript-262 v3.
Second, to get rid of the "http%3A//" do not use location.href
, but assemble the location
properties host
, pathname
, search
and hash
instead:
encodeURIComponent(location.host + location.pathname + location.search + location.hash);
Upvotes: 0
Reputation: 504
In most browsers, the referrer is sent as a standard field of the HTTP protocol. This technically isn't the answer to your question, but it would be a cleaner and less conspicuous solution to grab that information server-side.
In PHP, for example, you could write:
$ref = $_SERVER['HTTP_REFERER'];
...and then store that in a text file or a database or what-have-you. I can't really tell what your end purpose is, because clicking a bookmark lacks the continuity of browsing that necessitates referrer information (like the way that moving from a search engine or a competitor's website would). They could be coming from a history of zero, from another page on your site or something unrelated altogether.
Upvotes: 1