jQuery即时URL缩短

编程入门 行业动态 更新时间:2024-10-16 18:37:08
本文介绍了jQuery即时URL缩短的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一种即时网址缩短器,就像tweetdeck的工作原理一样。我找到了许多jQuery和一般的javascript插件,它们接受一个url并在按下按钮时通过缩短服务(如bit.ly)运行它。但是,我还没能找到一个在飞行中做到这一点。我的第一个问题是这已经存在于某个地方吗?其次,如果没有,那么识别需要在文本框中缩短的URL的最佳方法是什么?我的想法:

I'm looking for an on the fly URL shortener much like how tweetdeck works. I have found many jQuery and general javascript plugins that take a url and run it through a shortening service such as bit.ly when a button is pressed. However, I have not been able to find one that does it on the fly. My first question is does this already exist someplace? Secondly, if it doesn't, then what would be the best way to recognize a URL that needs to be shortened inside a textbox? My thoughts:

  • 在该文本区域的onKeyUp上运行查找http的文本
  • 如果找到抓取整个网址(如何确定结尾?可以是句号,逗号,空格等...)
  • 确保网址不是bit.ly网址
  • 验证URL(发出请求并确保http响应不是错误,bit.ly已经这样做了吗?)
  • 如果有效,将网址发送到bit.ly的API并获得回复
  • 将长网址替换为文本区域中的短网址。
  • On onKeyUp of that text area run through the text looking for http
  • If found grab the whole URL (how do I determine the end? could be period, comma, space, etc...)
  • Make sure the URL isn't already a bit.ly URL
  • Validate the URL (make a request and make sure the http response is not an error, does bit.ly already do this?)
  • If valid, send the url to bit.ly's API and get the response
  • Replace the long URL with the short URL in the text area.
  • 想法?

    推荐答案

    以下是如何使用Bitly获取缩短网址的示例API和jQuery:

    Here is an example how to get a shortened URL with Bitly API and jQuery:

    function get_short_url(long_url, login, api_key, func) { $.getJSON( "api.bitly/v3/shorten?callback=?", { "format": "json", "apiKey": api_key, "login": login, "longUrl": long_url }, function(response) { func(response.data.url); } ); }

    以下代码可用于获取简短的网址:

    The following code could be used to get a short URL:

    /* Sign up for Bitly account at bitly/a/sign_up and upon completion visit bitly/a/your_api_key/ to get "login" and "api_key" values */ var login = "LOGIN_HERE"; var api_key = "API_KEY_HERE"; var long_url = "www.kozlenko.info"; get_short_url(long_url, login, api_key, function(short_url) { console.log(short_url); });

    更多推荐

    jQuery即时URL缩短

    本文发布于:2023-11-28 21:20:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1643982.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:jQuery   URL

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!