www.webdeveloper.com
Results 1 to 8 of 8

Thread: Javascript code to copy and paste to a clipboard

  1. #1
    Join Date
    Oct 2008
    Posts
    3

    Javascript code to copy and paste to a clipboard

    <html>
    <textarea name="textarea" id="ilink">choose and copy contents</textarea>
    <br /><br/>
    <input type="button" value="Select Text" onClick="SelectContents(ilink)">

    <a href="javascript:SelectContents('ipwd')">Select and Copy Password</a>

    <script language=javascript>
    function SelectContents(ilink)
    {
    document.getElementById("ilink").select();
    var copyrange=document.getElementById("ilink").createTextRange();
    copyrange.execCommand("Copy");
    }
    </script>
    <html>

    the above code copies contents from the textarea when we click on the "selelct all" anchor tag. But sadly this code works only for IE browser.how to ensure that this works for mozilla also. thanks for any help.

  2. #2
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Browsers other than IE have NO ACCESS to the clipboard since it isn't part of your web page and the operating system may not even have such a thing.

    You can enable it in the browser when you are using Firefox on an intranet but it is a security risk to do sofor the internet.

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by felgall View Post
    Browsers other than IE have NO ACCESS to the clipboard since it isn't part of your web page and the operating system may not even have such a thing.

    You can enable it in the browser when you are using Firefox on an intranet but it is a security risk to do sofor the internet.
    your'e gonna love this one felgall!

    it works in firefox (or any browser really) to set the clipboard, no user config changes needed!
    it is also nice that it requires no user confirmation or warning about the clipboard, simply sets the text.
    big downside is that is limited to the amount of into it can handle. this varies by browser, but should always be at least 2kb.

    thank you flash.


    Code:
    function copy(str) {
       var D=document;
        if(!copy.div) { copy.div = D.createElement('div'); D.body.appendChild(copy.div);  }
     
    flashVar = "Q1dTB3kAAAB4nKtgYI1nYOBfwMDAw8jgzPT//3975lAGBoYOdQYWhu\
    SczIKk/MSiFIac1Lz0kgyG4MriktRchuLUEme41DQmBg4GGRDJ6Cc0l4l  BAibCzsDO\
    CDSJgwksyRwkzuAA5AIAd7oY/w==";
     
    copy.div.innerHTML =  '<embed src="'+flashVar+'" FlashVars="clipboard='+encodeURIComponent(str) +
        '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    }
    use like: copy(document.getElmementById("ilink").value);




    -----
    one more thing (about the non-flash way):
    i made the config change to unlock the clipboard in firefox a long time ago.
    just change signed.applets.codebase_principal_support to true in about:config.
    it's not a security risk (imho) because you still get a confirmation prompt whenever a site tries to get to the clipboard.
    i have NEVER had the confirm box pop-up on a page i didn't expect it to.
    if it ever did, i would simply click "no", and the javascript would get denied clipboard access.
    Last edited by rnd me; 10-09-2008 at 05:25 PM.

  4. #4
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    That is flash accessing the clipboard - not JavaScript.

    It wouldn't work on ANY of the browsers on my computer because I have Flash turned off.

    It also wouldn't work on any operating system that doesn't have a clipboard or where the browser doesn't support flash (which would include most browsers not running on a computer).

  5. #5
    Join Date
    Oct 2008
    Posts
    3

    Unhappy copy and paste in javascript

    function copy(str) {
    var D=document;
    if(!copy.div) { copy.div = D.createElement('div'); D.body.appendChild(copy.div); }

    flashVar = "Q1dTB3kAAAB4nKtgYI1nYOBfwMDAw8jgzPT//3975lAGBoYOdQYWhu\
    SczIKk/MSiFIac1Lz0kgyG4MriktRchuLUEme41DQmBg4GGRDJ6Cc0l4l BAibCzsDO\
    CDSJgwksyRwkzuAA5AIAd7oY/w==";

    copy.div.innerHTML = '<embed src="'+flashVar+'" FlashVars="clipboard='+encodeURIComponent(str) +
    '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    }


    can u pls explain the meaning of this code?
    also which is the replacement statement of document.getelementbyid("ilink").
    means which is ilink here?

    also i changed the setting in about:config,but still the clipboard didnt work.

  6. #6
    Join Date
    Oct 2008
    Posts
    3
    Hi,
    in this link ,"http://developer.mozilla.org/en/Using_the_Clipboard",i found the following code:

    const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].
    getService(Components.interfaces.nsIClipboardHelper);
    gClipboardHelper.copyString("Put me on the clipboard, please.");


    but how do i implement this code in php?

  7. #7
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Quote Originally Posted by swetha13 View Post
    but how do i implement this code in php?
    PHP is running on the server and there isn't a clipboard on the web server for it to access (there may be a clipboard on the computer itself but the web server software can't access it).

  8. #8
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by felgall View Post
    That is flash accessing the clipboard - not JavaScript.

    It wouldn't work on ANY of the browsers on my computer because I have Flash turned off.

    It also wouldn't work on any operating system that doesn't have a clipboard or where the browser doesn't support flash (which would include most browsers not running on a computer).
    I know it's not javascript, but a lot more people have flash (~90%) than a non IE browser (~35%).

    didn't consider OS, what OS doesn't have a clipboard?



    OP:
    throw the copy function somewhere along side your other functions in the script.

    then replace the function SelectContents with this new one:

    Code:
    function SelectContents(ilink)
    {
      var elm = document.getElementById("ilink");
      elm.select();
    if(window.ActiveXObject){
       var copyrange=document.getElementById("ilink").createTextRange();
       copyrange.execCommand("Copy");
    } else {
      copy(elm.value);  
    }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles