0

I'm listening to a Ctrl-C event and creating a JSON, I'll need this JSON in a different part of my application where I intend to use the JSON for a custom paste on Ctrl-V; but I don't have any reference here to the previous part, where the copy was done. Not even a global variable (long story).

Basically, these Ctrl-C and Ctrl-V events are supposed to copy and paste complex custom-made HTML elements, with their own events and keyframe animations, from one part of the application to the other. Working with the JSON is the only way to go.

I did some digging and came across questions like this, but they only address the case of putting selected text on the clipboard. I'm looking for something which can put custom content on the clipboard of the system or the browser.

Is there any inbuilt method, or an external utility or plugin, that would help me achieve this?

Thanks.

EDIT: This copy-paste feature will be used on a website by users, not just developers. Therefore, we can't assume anything. Cookies won't work because cookies might be turned off. localstorage or sessionstorage won't work because the application may be open for different projects in different windows; and the user might want to copy from one window and paste into another.

Community
  • 1
  • 1
Snowman
  • 2,465
  • 6
  • 21
  • 32
  • you could always use `localstorage` or `sessionstorage` depending on the lifetime required. although this may not work in older browsers. and will mean the user cannot paste in content from external sources – Martin Glennon-Brown May 04 '16 at 09:47
  • @MartinGlennon: Nice idea, but the user may want to paste the content on a different tab open in a different Window. – Snowman May 04 '16 at 11:11
  • @MartinGlennon: Going by casual research on console, it looks like localstorage works even with different windows. Thanks. I'll try with my actual requirement, and if it works, I'm willing to accept your answer (but you'll have to post it as an answer). – Snowman May 04 '16 at 11:26
  • when i say external i mean not from a browser – Martin Glennon-Brown May 04 '16 at 14:24

2 Answers2

0

You could look in to cookies here using jQuery:

// You can seriaize the data as JSON, like this:
$.cookie("property-name", JSON.stringify($("#foo").data()));

// Get data from the cookie:
$("#foo").data(JSON.parse($.cookie("property-name")));
Riddell
  • 1,429
  • 11
  • 22
  • Thanks for answering, but using cookies isn't a dependable solution for me as the user might have turned her cookies off. – Snowman May 04 '16 at 11:08
0

you could always use localstorage or sessionstorage depending on the lifetime required. although this may not work in older browsers. and will mean the user cannot paste in content from external sources