Imitation of copies of copies of modern browsers using jQuery (add http: // when copying)

I am trying to imitate the functionality of the "copy url" browser.

When viewing the url you do not see http:// . If you copy it to the clipboard, http:// will be added. So far I have this:

 $('#address input').bind('copy', function() { $.fn.changevalue = function (v) { return $(this).val(v).trigger('change'); } var origval = $(this).val(); $(this).changevalue('http://' + origval); }); 

This adds http:// to the input field if a "copy" action is detected, but it stops copying to the clipboard. The function also adds http:// to the main input field, which I don't want. I just want to add it to the clipboard.

+4
source share
2 answers

Note. JavaScript cannot access the buffer. Any plugin that does this usually uses Flash in the background.

To capture a copy event, after adding http:// to the input, you need to fire the select event (not the change event). When the value changes, the text is no longer selected, so you need to select a new text after editing it.

To remove http:// from the input field, you can add setTimeout at the end of the event to the reset value.

In addition, you must set $.fn.changevalue outside the callback, it does not need to be reinstalled for each copy event.

 $.fn.changevalue = function(v) { return this.val(v).trigger('select'); // this is already a jQuery object } $('#address input').bind('copy', function() { var $this = $(this), origval = $this.val(); $this.changevalue('http://' + origval); setTimeout(function(){ $this.val(origval); }, 0); }); 

Demo: http://jsfiddle.net/TGHcD/

+3
source

I can't find any "copy" event in jQuery, and it seems a bit complicated to get it working with cross browsers.

See this topic / question for plugins and other ways to copy text to the clipboard. (Some use flash (swf files) to make it work.)

Additional information on the topic:

0
source

Source: https://habr.com/ru/post/1386337/


All Articles