1

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

6
  • Please don't do this. No offense, but if a user would drag this link to his browsers toolbar as a bookmark, this would act kind of spyware. The location.href you escape (which is obsolete btw, you should use encodeURIComponent()), will be evaluated at runtime, i.e. if a user clicks this bookmark, the location.href of the page he is currently viewing would be sent, which is most likely not the same page as at the time he bookmarked one of your pages. Commented Apr 25, 2011 at 23:27
  • I'm not storing it or anything, I just need to know where he comes from because what is going to be displayed depends on where he comes from. Commented Apr 26, 2011 at 0:05
  • I need to send the page he is currently viewing not the one where he created the bookmark. Could using location.ref and escape cause me problems? What would be the proper method to do it? Commented Apr 26, 2011 at 0:07
  • As of now the bookmark is working as it should, I just want to remove the "http%3A//" so it's cleaner! Commented Apr 26, 2011 at 0:08
  • Yes, using location.href can cause privacy problems, when used in a bookmark. Imagine a user is viewing a page that he definitely wants to keep private to himself. Clicking the bookmark while viewing that private page would expose it to your website. I doubt all users are aware what they send when clicking such bookmark, as there was another page context active the time dragging the link into the toolbar. Commented Apr 26, 2011 at 0:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.