document.execCommand throws exception in Firefox

Hello,

I want to allow users to easily save text on one of our pages to their desktop as a text file. I have a button which calls this function when clicked:

function saveAsMe(button)
      {
      var filename="file.txt";
      var formName = button.form.name;
      if (formName == "form_step_email_invite_text_initial")
            filename="survey_invite_initial.txt";
      else if (formName == "form_step_email_invite_text_reminder")
            filename="survey_invite_reminder.txt";
      else if (formName == "form_step_email_invite_text_last")
            filename="survey_invite_last.txt";
      document.execCommand('SaveAs',null,filename);
      }


Works like a charm in IE. But not in Firefox (I'm using v1.0.4). Throws this exception:

Error: uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]"  nsresult: "0x80004005 (NS_ERROR_FAILURE)"  location: "JS frame :: [URL] :: saveAsMe :: line 224"  data: no]


Does firefox not support document.execCommand()? Is there another method for allowing the user to save the text of a page to the file name and location of their choosing?

Thanks for your help.

Leah
lzagreusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

justinbilligCommented:
Firefox ( and all other gecko browsers ) doesnt support execCommand.

Yes, telling the user to click file -> save as

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lzagreusAuthor Commented:
OK, thanks for the prompt answer. Another thought: can I make JavaScript mimic a key press, such as Ctrl+S?
lzagreusAuthor Commented:
It's not really important to answer the key press question (unless you have the answer at your fingertips). I worked around the problem by putting a try-catch block around the call to document.execCommand(). The catch block has an alert suggesting the user either copy-and-paste the text or use file > save as.

Thanks for your help!

Leah
justinbilligCommented:
Sounds good. Glad I could help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.