views:

104

answers:

4

Hi!

I'm a total newbie when it comes to Javascript/jQuery so hopefully you can help me.

I have a textbox where most people are gonna paste links into. Is there a nice way for the textbox to detect that a link has been pasted (starts with http://) and then remove the protocol dynamically in the textbox without having to push a button or so afterwards?

+1  A: 

You can use string.replace(), basic JavaScript really, it actually accepts a regex so string.replace(/^http:\/\//,"") should suffice.

Lloyd
This also removes `http://` when it's in the middle of a string
Marcel Korpel
That's very true yes, guess you have to do something like a regex ^http://
Lloyd
Added the regex version, this should do as thought.
Lloyd
+1  A: 

Assuming you want it stripped out as soon as the user types/pastes it:

Lets say for arguments sake that you've given the text input the id "url":

$(document).ready(function(){

  var timer;

  $("#url").live("keyup",function(){
       clearTimeout(timer);

       timer = setTimeout(function(){
           var textbox = $("#url");
           if (textbox.val().indexOf("https://") == 0)
               textbox.val(textbox.val().substring(8));
           if (textbox.val().indexOf("http://") == 0)
               textbox.val(textbox.val().substring(7));
       },500);
   });
});​

As mentioned by @Marcel Korpel in the comments this should now take care of what you want.

Ryan
You could also do this `onkeypress` in combination with a timer (of (e.g.) one second, so one second after the last key is pressed the form field changes). And yes, I've seen URLs with `http://` in the middle of it, when using a site as some kind of proxy (to access sites from another IP address that has a subscription; e.g.: `https://www.example.com/access/http://hiddensite.org`).
Marcel Korpel
@Marcel: please see my revised post above and let me know if that works. I set the delay to be only half a second as opposed to a full second but that is easily changed.
Ryan
@Ryan: this [works](http://jsbin.com/ihopu/edit#preview), though I'm not sure if this works for the OP's actual problem ;), as it strips the protocol regardless of the fact it's `http:` or `https:`, making several sites inaccessible (when reattaching `http:` in the output).
Marcel Korpel
With some slight modifications this worked very well. Thank you!
Anders Forslund
+1  A: 

This will check to make sure that it starts with http:// and then replaces that.

$(document).ready(
    function()
    {
       $("#url").change(
           function()
           {
               var textbox = $(this);
               if (textbox.val().indexOf("http://") == 0)
                   textbox.val(textbox.val().substring(7));
           });
    });​

Here is also a working example to check out: http://jsfiddle.net/epwDA/4/

spinon
Thanks! Not quite as good as Ryan's solution but this also worked as a charm. Will mark this answer as useful when I can (apparently im too new to this site to do that yet..)And thanks for the tip about jfiddle
Anders Forslund
A: 

Thanks for all the answers. I will try it tomorrow. I think Ryan's example is the best for me since you say it strips it directly at the paste. And i didnt mention it but it is suppose to work with both http and https (i know which one to reattach afterwards).

I´ll be back with some feedback!

Anders Forslund
Great but this isn't an answer; you should post this as a comment, probably to Ryan's answer. That said, good to know you're aware of the http/https issue.
Marcel Korpel
Done. I'm new to this site so you will have to excuse me :)
Anders Forslund
@Anders: No problem. :) Now you can delete this 'answer' (use the `delete` option below your answer).
Marcel Korpel
BTW, I see you made two separate accounts here? Why?
Marcel Korpel