MesaFloyd Posted May 21, 2008 Share Posted May 21, 2008 I am a 'rookie' please be gentile. I have a website in PHP with some HTM/L. I display a visitors IP address by using this <?php echo $_SERVER['REMOTE_ADDR'] ; ?> All works fine, I simply want to add a <Copy to Clipboard> button <or link> that when pressed copies that IP to a the users clipboard (I do not want him to highlight then <ctl-C>... My problem is, I see several solutions, but most do not function in both IE and Firefox... I have also seen solutions that require a flash plugin, which is not desireable... Can anyone suggest a clean and simple solution. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted May 21, 2008 Share Posted May 21, 2008 Firefox security will not allow unsigned javascripts to copy to the clipboard, so you either have to sign your javascript, or get your users to configure their firefoxes "about:config" to allow copy to clipboard. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 21, 2008 Share Posted May 21, 2008 Step 1: download this file keep name as "copy.swf" (right click link "Save"): http://tzfiles.com/javascripts/copy.swf Step 2: Upload "copy.swf" to your server. Step 3: Save this code in a file called "copy.js" place it in the same folder as "copy.swf" on the server function highlight(field) { field.focus(); field.select(); var urlSwf = "/javascripts/copy.swf"; window.status = 'Copied text to clipboard'; // Copy the text inside the text box to the user's clipboard var flashcopier = 'flashcopier'; var divholder = document.createElement('div'); divholder.id = flashcopier; document.body.appendChild(divholder); divholder.innerHTML = ''; var divinfo = '<embed src="' + urlSwf + '" FlashVars="clipboard='+escape(field.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>'; divholder.innerHTML = divinfo; return false; } Step 4: change this var urlSwf = "/javascripts/copy.swf"; to the location of you swf file (Line 4) step 5: add this to the head of your document (replace src with the location of your "copy.js"): <script type="text/javascript" src="/javascripts/copy.js"></script> Step 6: Add this in the body of your document: <input id="addr" value="<?= $_SERVER['REMOTE_ADDR']; ?>" type="text"> <input type="button" onclick="highlight(getElementById('addr'));" value="Copy" /> Step 7: Give it a try in your browser! Quote Link to comment Share on other sites More sharing options...
MesaFloyd Posted May 21, 2008 Author Share Posted May 21, 2008 Little Guy I followed your <excellent> instructions... and I think your code works great! For those <dummies like me> just some details... When I first saved the file, I saved it as .html and the php code appeard in the box... not the IP address... even so, when I push the copy button, it DID copy the code... Here is what appears in the box and what is copied.. <?= $_SERVER['REMOTE_ADDR']; ?> Then, I had an "ipifiny"(sp) ... duh! , I changed the file name to .php... and viola, it works. I will now integreate it on the working web page(I tested an experiment page) I'm promoting you to "THE BIG GUY"... ub da man! Thanks so much (BTW-new to this, I don't know how to tag this as SOLVED! so I put it in the headder, hope thats OK) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 21, 2008 Share Posted May 21, 2008 Your Welcome! SOLVED is at the bottom of the page just above "Quick Reply" on the left. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.