Search code examples
javascripturlsubmission

What's cleanest, shortest Javascript to submit a URL the user is at to another process via URL?


Like the Delicious submission bookmark-let, I'd like to have some standard JavaScript I can use to submit any visited URL to a 3rd party site when that's possible by URL. Suggestions?

For example, I've been using

javascript:void(location.href="http://www.yacktrack.com/home?query="+encodeURI(location.href)) 

so far but wonder if there's something more sophisticated I could use or better practice.


Solution

  • Do you want something exactly like the Delicious bookmarklet (as in, something the user actively clicks on to submit the URL)? If so, you could probably just copy their code and replace the target URL:

    javascript:(function(){
        location.href='http://example.com/your-script.php?url='+
        encodeURIComponent(window.location.href)+
        '&title='+encodeURIComponent(document.title)
    })()
    

    You may need to change the query string names, etc., to match what your script expects.

    If you want to track a user through your website automatically, this probably won't be possible. You'd need to request the URL with AJAX, but the web browser won't allow Javascript to make a request outside of the originating domain. Maybe it's possible with iframe trickery.

    Edit: John beat me to it.