0

I have a showPreview option for an entered URL on my site just like Facebook shows a preview of the URL.

When the User enters the url, the following condition gets verified:

$('#text').keyup(function(e) {
            allowPosting = true;
    if ((e.which === 13 || e.which === 32 || e.which === 17) && trim($(this).val()) !== "") 
}

Key code:

13 - Enter
45 - Insert
17 - control

Now the function is being called when the user copy and pastes the URL or Selects the URL from the preview and presses Enter key

What I want:

The preview should be shown if the user types in the URL, even if the user does not press the enter key or copy and pastes the URL.

What should I do?

3 Answers 3

1

Here is your code, this will do everything you wrote:

 $('#text').keyup(function(e) {
    allowPosting = true;
    if (/(^|\s)((http:\/\/|https:\/\/)[a-zA-Z0-9!*'();:@&=+$,\/?#[\]\-_.~]+)/.test($("#text").val()) && trim($(this).val()) !== "")
    {
        // DO STUFF
    }
}); 

It will also check if the input is a link or not, so that the user can enter "http://google.com/" and not e.g. "asdf"!

Sign up to request clarification or add additional context in comments.

Comments

1

check the .val() after every keyup, not only on Enter, and if it is an URL, do the rest

Comments

1

Try change in place of keyup :

$('#text').change(function(){...});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.