javascript help ''copy to clipboard'' button

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    javascript help ''copy to clipboard'' button

    I got not much clue about javascript, so i need help.

    I get some result in PHP, something like this:
    PHP Code:
    echo "Here is your result:<br/><input type='text' size='50' value='$final_result'>";
    ///// Need copy to clipboard BUTTON here for value $final_result 
    I've found some code on google, but got no clu how to use it.
    Code:
    <script language="JavaScript">
    <!-- Begin
    function copyit(theField) {
    var tempval=eval("document."+theField)
    tempval.focus()
    tempval.select()
    therange=tempval.createTextRange()
    therange.execCommand("Copy")
    }
    //  End -->
    </script>
    Code:
    <form name="aspinc">
    <pre>
    <div align="center">
    <input onclick="copyit('aspinc.select1')" type="button" value="Highlight All" name="cpy">
    <p>
    <textarea class="codebox-sm" name="select1">
    Text you want to highlight goes here.
    </textarea>
    </p>
    </pre>
    </div>
    </form>
    where and how to place this javascript ?
    Last edited by metulj; 10.11.13, 13:30.
    It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt.
    ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ !
    ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.
    キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

    #2
    javascript can be placed directly into the html document,
    btw. js copy to clipboard doesnt work in all browsers, there are advanced solutions which work with flash, but flash is not supported by mobiles
    Advertise your mobile site for FREE with AdTwirl

    Comment


      #3
      so... theres no proper solution to ''satisfie'' all browsers ?
      it'll have to javascript then,
      as mobile browsers are more important to me
      is there any better solution than posted above ?
      Last edited by metulj; 10.11.13, 13:59.
      It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt.
      ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ !
      ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.
      キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

      Comment


        #4
        I think with javascript copy functions it is impossible to do unless you have browser permissions (such permissions you can get on addons only) - i could be wrong.

        Most people end up using flash copying programs such as zero clipboard to get around the problem.

        but you can try as i maybe wrong and not up to date with browsers..... something like this:

        PHP Code:
        <html>
        <head>
        <script language="JavaScript">
        <!-- Begin
        function copyit(theField) {
        var tempval=eval("document."+theField)
        tempval.focus()
        tempval.select()
        therange=tempval.createTextRange()
        therange.execCommand("Copy")
        }
        //  End -->
        </script>
        </head>


        <?php
        echo '<form name="yourFormName">';
        echo 
        "Here is your result:<br/><input type='text' size='50' value='$final_result' name='inputName'>";
        echo 
        "<button onclick=\"copyit('yourFormName.inputName');\">Copy</button>";
        echo 
        "</form>";
        ?>
        Added after 18 minutes:

        by the looks of things this would only work in IE and safari
        for mobiles its a no - closest thing you could do is highlight all via javascript for users to press the copy button on there phones - making it slightly less of a task
        Last edited by something else; 10.11.13, 14:27.

        Comment


          #5
          Originally posted by something else View Post
          I think with javascript copy functions it is impossible to do unless you have browser permissions (such permissions you can get on addons only) - i could be wrong.

          Most people end up using flash copying programs such as zero clipboard to get around the problem.

          but you can try as i maybe wrong and not up to date with browsers..... something like this:

          PHP Code:
          <html>
          <head>
          <script language="JavaScript">
          <!-- Begin
          function copyit(theField) {
          var tempval=eval("document."+theField)
          tempval.focus()
          tempval.select()
          therange=tempval.createTextRange()
          therange.execCommand("Copy")
          }
          //  End -->
          </script>
          </head>


          <?php
          echo '<form name="yourFormName">';
          echo 
          "Here is your result:<br/><input type='text' size='50' value='$final_result' name='inputName'>";
          echo 
          "<button onclick=\"copyit('yourFormName.inputName');\">Copy</button>";
          echo 
          "</form>";
          ?>
          Added after 18 minutes:

          by the looks of things this would only work in IE and safari
          for mobiles its a no - closest thing you could do is highlight all via javascript for users to press the copy button on there phones - making it slightly less of a task
          exactly, LOL it works, but only in IE, didnt even tried in safari
          I ned some better solution
          It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt.
          ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ !
          ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.
          キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

          Comment


            #6
            havent tried this on mobile but here is how i do it on chrome extensions to highlight text for copying:
            PHP Code:
            <input type="text" onclick="this.focus();this.select();" /> 
            Last edited by something else; 10.11.13, 14:58.

            Comment


              #7
              Originally posted by something else View Post
              havent tried this on mobile but here is how i do it on chrome extensions to highlight text for copying:
              PHP Code:
              <input type="text" onclick="this.focus();this.select();" /> 
              Well, this works even on android default browser,
              so it kind a better solution then the first one
              Thanks
              It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt.
              ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ !
              ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.
              キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

              Comment

              Working...
              X