PDA

View Full Version : Multiline Variable



jfreak53
05-21-2008, 11:26 PM
I am looking to make a multiline variable, why you ask, it's quite simple, I have a copy to clipboard function for files that copies embed script to insert somewhere else in a system and it makes it so much easier at runtime if it's multiline and well formated like all programming should be if I have to edit it and well I just like it that way, call me anal retentive but I do. Ahhh the good ol days in programming when everything had to be in a certain place or it wouldn't work, not HTML you just slap things anywhere you want and it works, haha here I go rambling again. Anyways this is what I have so far:


var finals="<script type='text/javascript' src=\"http://www.microtronix-tech.com/tbionline/lib/wmvplayer/silverlight.js\"><\/script>\
<script type='text/javascript' src=\"http://www.microtronix-tech.com/tbionline/lib/wmvplayer/wmvplayer.js\"><\/script>\
\
<div id=\"container\"><\/div>\
\
<script type=\"text/javascript\">\
var cnt = document.getElementById(\"container\");\
var src = 'http://www.microtronix-tech.com/tbionline/lib/wmvplayer/wmvplayer.xaml';\
var cfg = {\
height:'334',\
width:'470',\
file:'" + s + "',\
backcolor:'000000',\
frontcolor:'FFFFFF',\
lightcolor:'FF0000',\
screencolor:'330000',\
logo:'http://www.microtronix-tech.com/tbionline/logo.png',\
showstop:'true',\
autostart:'true'\
};\
var ply = new jeroenwijering.Player(cnt,src,cfg);\
<\/script>";

As you can see I have escaped the end of each line to allow for multiline in the original code. What I cannot get it to do is to add multiline when it copies it, it still copies it as single line. So I tried \n, then \n\r, no avail. \n doesn't do anything and \n\r won't even work, gives me an error when I click on the copy button. Any ideas out there as to how I can do this? Thanks in advance for any help you can give me.

jscheuer1
05-22-2008, 03:04 PM
I have no idea about what you are saying as far as copying it goes. I can tell you it is a variable, but it is also an object. Escaping the the end of the lines doesn't change its properties or their names. If anything it would simply make the entire thing invalid as an object, but that would depend upon the script parser. As long as it is still a valid object, javascript sees it all on one line in memory.

If you want javascript to assign something with line breaks to the value of a textarea, the line breaks must be preserved, not escaped. If you want to write them out to another element, like a division, they must be replaced by the <br> tag by the script as it is writing. If you are taking the contents of an object and enumerating them for inclusion in a textarea's value, line breaks must be added as desired during the process. If to another element, once again the <br> tag.

jfreak53
05-24-2008, 04:45 PM
Maybe I didn't explain this good enough. I already have the copy to clipboard function working, that is not the problem, it even works escaping the line break like i have it right now. That is not my problem. My problem is I want the line breaks to go with it when it copies, and <br> won't work bc it's script it won't be used to display in a page, I want the line breaks for me at code time it makes it easier. I know that \n exists to make a line break and \r, but neither work for me when I use them, that info is in previous post. What else is there to have javascript make the line breaks without using HTML code since this will not be used in HTML for displaying purposes.

jscheuer1
05-25-2008, 04:15 AM
You still haven't made it all that clear how/where you (or anyone?) will see the code generated by your paste operation. I am assuming that since there is a copy, there must be a paste before anything can be seen as a result of the copy. Questions:


Is there a paste?

If so, who sees the paste - just you, or all/some users as well?

If there is a paste, where does it go?


If there is a paste, and you want that paste formated in a certain way, it would be at the time of the paste, not at the time of the copy where line breaks could most effectively be inserted. From what you are saying, this would most likely take the form of a regular expression replace on the comma and the end of the string:


string_value.replace(/,/g, ',\n').replace(/( };)$/, '\n$1')

For example, if you had:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>
<script type="text/javascript">
var string_value="var cfg = {\
height:'334',\
width:'470',\
file:'\" + s + \"',\
backcolor:'000000',\
frontcolor:'FFFFFF',\
lightcolor:'FF0000',\
screencolor:'330000',\
logo:'http://www.microtronix-tech.com/tbionline/logo.png',\
showstop:'true',\
autostart:'true'\
};"

document.write(string_value.replace(/,/g, ',\n').replace(/( };)$/, '\n$1'));
</script>
</body>
</html>

This would be the generated source code of that page:


<html><head><title></title>



<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body>
<script type="text/javascript">
var string_value="var cfg = {\
height:'334',\
width:'470',\
file:'\" + s + \"',\
backcolor:'000000',\
frontcolor:'FFFFFF',\
lightcolor:'FF0000',\
screencolor:'330000',\
logo:'http://www.microtronix-tech.com/tbionline/logo.png',\
showstop:'true',\
autostart:'true'\
};"

document.write(string_value.replace(/,/g, ',\n').replace(/( };)$/, '\n$1'));
</script>var cfg = { height:'334',
width:'470',
file:'" + s + "',
backcolor:'000000',
frontcolor:'FFFFFF',
lightcolor:'FF0000',
screencolor:'330000',
logo:'http://www.microtronix-tech.com/tbionline/logo.png',
showstop:'true',
autostart:'true'
};
</body></html>

jfreak53
05-26-2008, 10:40 PM
1. The only paste is when I at another web page paste the code using windows edit paste command. There is no formal code for pasting and won't be. This copy command is used for me and me alone to make my job easier copying a lot of files addresses that I want to include the embed code already, it cuts a lot of steps for me.

2. The final paste is only ever seen by me into a WYSIWYG editor, and the new line formatting is to make it easier of course to edit in the future.

3. The paste is made manually no script involved in a WYSIWYG editor that the editor then saves with full HTML of course to a database for display later. The script itself is never displayed the only thing displayed is the final embedded video.

So based on this information the new line break would have to happen during the copy before it makes it to the local clipboard.

jscheuer1
05-27-2008, 11:43 AM
How are you copying then (perhaps document.execCommand("Copy"))? Any 'real' copy would preserve existing line breaks, unless you are actually copying a representation of the code as seen on a page in the browser. In which case, prior to copying, <br> tags should be inserted (they can be removed from the original right after copying, and should only appear as line breaks in the copy that gets to the clipboard).

jfreak53
05-31-2008, 02:46 AM
Here is my copy code, it is kind of a weird code since it also has to work in firefox.


function copyToClipboardvid(s)
{

var finals="<script type='text/javascript' src=\"http://www.microtronix-tech.com/tbionline/lib/wmvplayer/silverlight.js\"><\/script>\
<script type='text/javascript' src=\"http://www.microtronix-tech.com/tbionline/lib/wmvplayer/wmvplayer.js\"><\/script>\
\
<div id=\"container\"><\/div>\
\
<script type=\"text/javascript\">\
var cnt = document.getElementById(\"container\");\
var src = 'http://www.microtronix-tech.com/tbionline/lib/wmvplayer/wmvplayer.xaml';\
var cfg = {\
height:'334',\
width:'470',\
file:'" + s + "',\
backcolor:'000000',\
frontcolor:'FFFFFF',\
lightcolor:'FF0000',\
screencolor:'330000',\
logo:'http://www.microtronix-tech.com/tbionline/logo.png',\
showstop:'true',\
autostart:'true'\
};\
var ply = new jeroenwijering.Player(cnt,src,cfg);\
<\/script>";
var s=finals;

if( window.clipboardData && clipboardData.setData )
{
clipboardData.setData("Text", s);
}
else
{
// You have to sign the code to enable this or allow the action in about:config by changing user_pref("signed.applets.codebase_principal_support", true);
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
if (!clip) return;

// create a transferable
var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
if (!trans) return;

// specify the data we wish to handle. Plaintext in this case.
trans.addDataFlavor('text/unicode');

// To get the data from the transferable we need two new objects
var str = new Object();
var len = new Object();

var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);

var copytext=s;

str.data=copytext;

trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);

var clipid=Components.interfaces.nsIClipboard;

if (!clip) return false;

clip.setData(trans,null,clipid.kGlobalClipboard);
}
}