最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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="="+encodeURI(location.href)) 

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

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./home?query="+encodeURI(location.href)) 

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

Share Improve this question edited Jun 25, 2017 at 10:29 ɢʀᴜɴᴛ 32.9k15 gold badges122 silver badges114 bronze badges asked Sep 10, 2008 at 15:35 Marshall KirkpatrickMarshall Kirkpatrick 2421 gold badge4 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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./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.

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

Another option would be to something like this:

<form action="http://www.yacktrack./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 bine the [save link] with the submit like this:

<form action="http://www.yacktrack./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./home" method="get" name="f">
  <input type="hidden" name="query" value="<%=Response.Url%>" />
  <input type="submit" name="Save Link" />
</form>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论