Doesn't work on Firefox

Discussion in 'Javascript' started by wayne@wengert.org, Jul 19, 2005.

  1. Guest

    I found the following script to copy text to the client clipboard but
    it is not working in Firefox (works fine in IE 6). Can anyone suggest
    what I need to change?

    =================================================
    function copy_clip(mytext){
    if (window.clipboardData)
    {
    // IE
    window.clipboardData.setData("Text", mytext);
    // Netscape
    }
    else if (window.netscape)
    {

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

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

    //
    trans.addDataFlavor('text/unicode');

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

    var copytext=mytext;
    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);
    }
    alert("Following info was copied to your clipboard:\n\n" + mytext);
    return false;
    }
    ==========================================
     
    , Jul 19, 2005
    #1
    1. Advertising

  2. RobG Guest

    wrote:
    > I found the following script to copy text to the client clipboard but
    > it is not working in Firefox (works fine in IE 6). Can anyone suggest
    > what I need to change?
    >
    > =================================================
    > function copy_clip(mytext){
    > if (window.clipboardData)


    Try this in non-IE browsers:

    alert( window.clipboardData )


    You will most likely get 'undefined'. Firefox et al don't support
    copying to the clipboard.


    [...]


    --
    Rob
     
    RobG, Jul 19, 2005
    #2
    1. Advertising

  3. Wayne W Guest

    Thanks. That is exactly what happened.

    So, I need to detect those browsers and disable the clipboard options?

    Wayne

    "RobG" <> wrote in message
    news:42dd0c38$0$6402$...
    > wrote:
    >> I found the following script to copy text to the client clipboard but
    >> it is not working in Firefox (works fine in IE 6). Can anyone suggest
    >> what I need to change?
    >>
    >> =================================================
    >> function copy_clip(mytext){
    >> if (window.clipboardData)

    >
    > Try this in non-IE browsers:
    >
    > alert( window.clipboardData )
    >
    >
    > You will most likely get 'undefined'. Firefox et al don't support copying
    > to the clipboard.
    >
    >
    > [...]
    >
    >
    > --
    > Rob
     
    Wayne W, Jul 19, 2005
    #3
  4. Baconbutty Guest

    Baconbutty, Jul 19, 2005
    #4
  5. Baconbutty Guest

    Sorry, forget my answer. I note you have got this covered in your code
    above.

    I would like to respectfully disagree with RobG.

    I think that Firefox does support clipboard operations (as did
    Mozilla), through XPConnect as you note.

    Your "if" statements should also discount other browsers, as you
    correctly do this by means of a "feature" test.

    I would think that your problem lies somewhere in your XPConnect code
    stated above. I have not had chance to look closely, but XUL Planet is
    a good source of information.
     
    Baconbutty, Jul 19, 2005
    #5
  6. Wayne W Guest

    Thanks for the response. The sample script I included supposidly uses that
    approach but obviously I missed something.

    Wayne

    "Baconbutty" <> wrote in message
    news:...
    > For Firefox, there is an alternative.
    >
    > Take a look at
    >
    > http://www.xulplanet.com/tutorials/xultu/clipboard.html
    >
     
    Wayne W, Jul 19, 2005
    #6
  7. Wayne W Guest

    Thanks.... I'll keep looking.

    Wayne

    "Baconbutty" <> wrote in message
    news:...
    > Sorry, forget my answer. I note you have got this covered in your code
    > above.
    >
    > I would like to respectfully disagree with RobG.
    >
    > I think that Firefox does support clipboard operations (as did
    > Mozilla), through XPConnect as you note.
    >
    > Your "if" statements should also discount other browsers, as you
    > correctly do this by means of a "feature" test.
    >
    > I would think that your problem lies somewhere in your XPConnect code
    > stated above. I have not had chance to look closely, but XUL Planet is
    > a good source of information.
    >
     
    Wayne W, Jul 19, 2005
    #7
  8. Wayne W Guest

    I re-coded the function to match the site you referenced (see below) and I
    see the alert("copytext set") alert
    but not the alert("Just tried to set str")? It appears that the step to set
    the value of "str" causes the function to terminate?
    I don't gey any error messages?

    Any thoughts?

    Wayne



    ==================================================
    function copy_clip(mytext){
    if (window.clipboardData)
    {
    // IE
    window.clipboardData.setData("Text", mytext);
    // Netscape
    }
    else if (window.netscape)
    {
    //
    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
    var copytext=mytext;
    alert("copytext set")
    var str = Components.classes["@mozilla.org/supports-string;1"].
    createInstance(Components.interfaces.nsISupportsString);
    alert("Just tried to set str")
    if (!str) return false;
    str.data = copytext;
    alert("Just set str.data")
    var trans = Components.classes["@mozilla.org/widget/transferable;1"].
    createInstance(Components.interfaces.nsITransferable);
    if (!trans) return false;
    alert("Just set trans")
    trans.addDataFlavor("text/unicode");
    trans.setTransferData("text/unicode",str,copytext.length * 2);

    var clipid = Components.interfaces.nsIClipboard;
    var clip =
    Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
    if (!clip) return false;

    clip.setData(trans,null,clipid.kGlobalClipboard);
    }
    alert("Following info was copied to your clipboard:\n\n" + mytext);
    return false
    }
    ===================================================
    "Baconbutty" <> wrote in message
    news:...
    > Sorry, forget my answer. I note you have got this covered in your code
    > above.
    >
    > I would like to respectfully disagree with RobG.
    >
    > I think that Firefox does support clipboard operations (as did
    > Mozilla), through XPConnect as you note.
    >
    > Your "if" statements should also discount other browsers, as you
    > correctly do this by means of a "feature" test.
    >
    > I would think that your problem lies somewhere in your XPConnect code
    > stated above. I have not had chance to look closely, but XUL Planet is
    > a good source of information.
    >
     
    Wayne W, Jul 19, 2005
    #8
  9. RobG Guest

    Wayne W wrote:
    > Thanks.... I'll keep looking.
    >


    Please don't top-post. Although that is the default behaviour of
    Outlook Express, but it's not the preferred layout of this group.

    Here are a couple of links regarding using the clipboard and Mozilla:

    <URL:http://www.infogears.com/cgi-bin/infogears/mozilla_firefox_copy_paste.html>

    <URL:http://www.mozilla.org/projects/security/components/per-file.html>

    Users can allow access to the clipboard on a per-site basis, however it
    requires modification of user preferences that are likely beyond the
    average web surfer. As a result you should expect that, in a general web
    context, clipboard support is not available.

    If your usage is for an intranet, and you can control a users' settings,
    then maybe you can make it work reliably. This newsgroup is intended to
    focus on JavaScript for the web, hence responses tend to bent in that
    direction.

    > Wayne
    >
    > "Baconbutty" <> wrote in message
    > news:...
    >
    >>Sorry, forget my answer. I note you have got this covered in your code
    >>above.
    >>
    >>I would like to respectfully disagree with RobG.


    Yes, I was a bit dismissive. :-x

    Mozilla does support the clipboard, but only through specific user
    actions to allow access to it. Page authors should not expect clipboard
    support to be available, and therefore should probably not attempt to
    use it. The links above show how to enable it - I don't think your
    average user is going to be able to do it even if you can convince them to.

    Support within an intranet may be able to be made more reliable.

    >>
    >>I think that Firefox does support clipboard operations (as did
    >>Mozilla), through XPConnect as you note.
    >>
    >>Your "if" statements should also discount other browsers, as you
    >>correctly do this by means of a "feature" test.


    The feature tests are insufficient - testing for 'window.netscape' does
    not ensure that PrivilegeManager.enablePrivilege is supported, or that
    the user will allow the privileges to be modified.

    That call should be inside a try..catch block in case the user refuses
    to change their privileges, or you can let them deal with the message
    starting:

    "A script from /your_domain/ is requesting enhanced abilities that
    are UNSAFE and could be used to compromise your machine or data:"

    [...]

    --
    Rob
     
    RobG, Jul 20, 2005
    #9
  10. Wayne W Guest

    Rob;

    Thanks for the very educational response.

    I'm not sure what "top posting" is nor how to prevent it?

    I'll do some more reading and re-think my general approach here. Too bad
    because, in my option, saving the user from having to select and the "copy"
    seems like a good thing. It is so easy to accomplish for IE.

    Oh well

    Wayne

    "RobG" <> wrote in message
    news:kRgDe.33$...
    > Wayne W wrote:
    >> Thanks.... I'll keep looking.
    >>

    >
    > Please don't top-post. Although that is the default behaviour of Outlook
    > Express, but it's not the preferred layout of this group.
    >
    > Here are a couple of links regarding using the clipboard and Mozilla:
    >
    > <URL:http://www.infogears.com/cgi-bin/infogears/mozilla_firefox_copy_paste.html>
    >
    > <URL:http://www.mozilla.org/projects/security/components/per-file.html>
    >
    > Users can allow access to the clipboard on a per-site basis, however it
    > requires modification of user preferences that are likely beyond the
    > average web surfer. As a result you should expect that, in a general web
    > context, clipboard support is not available.
    >
    > If your usage is for an intranet, and you can control a users' settings,
    > then maybe you can make it work reliably. This newsgroup is intended to
    > focus on JavaScript for the web, hence responses tend to bent in that
    > direction.
    >
    >> Wayne
    >>
    >> "Baconbutty" <> wrote in message
    >> news:...
    >>
    >>>Sorry, forget my answer. I note you have got this covered in your code
    >>>above.
    >>>
    >>>I would like to respectfully disagree with RobG.

    >
    > Yes, I was a bit dismissive. :-x
    >
    > Mozilla does support the clipboard, but only through specific user actions
    > to allow access to it. Page authors should not expect clipboard support
    > to be available, and therefore should probably not attempt to use it. The
    > links above show how to enable it - I don't think your average user is
    > going to be able to do it even if you can convince them to.
    >
    > Support within an intranet may be able to be made more reliable.
    >
    >>>
    >>>I think that Firefox does support clipboard operations (as did
    >>>Mozilla), through XPConnect as you note.
    >>>
    >>>Your "if" statements should also discount other browsers, as you
    >>>correctly do this by means of a "feature" test.

    >
    > The feature tests are insufficient - testing for 'window.netscape' does
    > not ensure that PrivilegeManager.enablePrivilege is supported, or that the
    > user will allow the privileges to be modified.
    >
    > That call should be inside a try..catch block in case the user refuses to
    > change their privileges, or you can let them deal with the message
    > starting:
    >
    > "A script from /your_domain/ is requesting enhanced abilities that
    > are UNSAFE and could be used to compromise your machine or data:"
    >
    > [...]
    >
    > --
    > Rob
     
    Wayne W, Jul 20, 2005
    #10
  11. Robi Guest

    Wayne W wrote:
    [...]
    > I'm not sure what "top posting" is nor how to prevent it?


    a message whith top posting looks like this:

    | Because it messes up the flow of reading.
    | > How come?
    | > > I prefer to reply inline.
    | > > > What do you do instead?
    | > > > > No.
    | > > > > > Do you like top-posting?

    IOW, readable like black ink on black paper.

    > I'll do some more reading and re-think my general approach here. Too bad
    > because, in my option, saving the user from having to select and the "copy"
    > seems like a good thing. It is so easy to accomplish for IE.
    >
    > Oh well

    [...]

    Inline and bottom posting are the preferred Newsgroup styles.

    You can prevent top posting by trimming the quoted text to the essential,
    reply inline (just after a section you want to comment on, answer or reply to)
    and/or at the end (or bottom) of the quoted (and hopefully now trimmed) text
    like I am doing right now.

    http://en.wikipedia.org/wiki/Top-posting
     
    Robi, Jul 20, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Freeman
    Replies:
    8
    Views:
    7,722
    tcena9
    Feb 16, 2011
  2. =?Utf-8?B?U3RldmUgRm9yZA==?=

    BackColor attribute in HyperLink doesn't work in Firefox

    =?Utf-8?B?U3RldmUgRm9yZA==?=, Oct 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    585
    =?Utf-8?B?U3RldmUgRm9yZA==?=
    Oct 10, 2005
  3. =?Utf-8?B?VHlsZXIgQ2FydmVy?=

    Programmatic Use of DefaultButton Doesn't Work in FireFox

    =?Utf-8?B?VHlsZXIgQ2FydmVy?=, Mar 9, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    3,831
    Yuan Ren[MSFT]
    Mar 15, 2006
  4. shellyshao

    hyperlinkfield doesn't work in firefox

    shellyshao, May 31, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    370
    shellyshao
    May 31, 2006
  5. John Paul
    Replies:
    2
    Views:
    1,544
    Ben C
    Oct 13, 2006
Loading...

Share This Page