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

StackOverflow https://stackoverflow.com/questions/54426

  •  09-06-2019
  •  | 
  •  

Question

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.

Was it helpful?

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.

OTHER TIPS

document.location = "http://url_submitting_to.com?query_string_param=" + window.location;

Another option would be to something like this:

<form action="http://www.yacktrack.com/home" method="get" name="f">
  <input type="hidden" name="query" />
</form>

then your javascript would be:

f.query.value=location.href; f.submit();

or you could combine the [save link] with the submit like this:

<form action="http://www.yacktrack.com/home" method="get" name="f" onsubmit="f.query.value=location.href;">
  <input type="hidden" name="query" />
  <input type="submit" name="Save Link" />
</form>

and if you're running server-side code, you can plug in the location so you can be JavaScript-free:

<form action="http://www.yacktrack.com/home" method="get" name="f">
  <input type="hidden" name="query" value="<%=Response.Url%>" />
  <input type="submit" name="Save Link" />
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top