简体   繁体   中英

Firefox document.all issue

I am trying to make my application compatible with all browsers and my code

 ClientScript.RegisterClientScriptBlock(this.GetType(), "theAction",
   "<script type='text/javascript'>
      function DoSave() {
        try {
          document.all('" + lbnSave.ClientID + "').click();
        } catch(e){}
      }
   </script>");

is working in IE8, Chrome and Opera, but not in Firefox. I know that document.all is IE specific, but how do I rewrite this to work in Firefox too? Many Thanks!

Use getElementById which is cross-browser:

 ClientScript.RegisterClientScriptBlock(this.GetType(), "theAction",
   "function DoSave() {
     try {
       document.getElementById('" + lbnSave.ClientID + "').click();
     } catch(e){}
   }", true);

Also, you can make your code cleaner. Fourth parameter set to true will add <script> tag automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM