www.webdeveloper.com
Results 1 to 5 of 5

Thread: RegExp works in Firefox, not IE

  1. #1
    Join Date
    May 2009
    Posts
    10

    RegExp works in Firefox, not IE

    I have a javascript function on my page named "print", that opens up a new window and a print dialog so the user can select a printer and print the page. The html itself is in a frame, so the print function extracts the innerHTML and then presents it to the user in the new window. After extracting the innerHTML, I'm attempting to remove any links to avoid having the user click on them (I just want the user to print). This works fine in FF (3.6.9), but causes IE (IE7 and 8) to lock up in a rather nasty manner. The "destination" in this case is simply the current page (e.g., index.aspx). Any ideas anyone? Thanks in advance!

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head> 
    <title>Test Print</title>
    <script type="text/javascript">
      function print()
      {
        var content_frame_source = "";
        var reg_exp = null;
        var new_window = null;
    
        try
        {      
          content_frame_source = window.frames["content_frame"].document.body.innerHTML;
        }
        catch(e)
        {
          alert("This page cannot automatically be printed. In Firefox, right click on the page, select 'This Frame', and then 'Open Frame in New Tab' before printing. In Internet Explorer, copy and paste the page content into another program to print.");
          return false;
        }
        if (content_frame_source != "")
        {
          new_window = window.open("", "", "toolbar=no, menubar=no, location=no, directories=no, status=no, copyhistory=no");
          new_window.document.open();
          new_window.document.write("<html><head><style type=\"text/css\">@import url(\"http://localhost:2599/css/style.css\"); .hide_from_print { display: none; }</style></head><body onload=\"window.print();\">");
          
          // This is the offending line (where I try to remove any links):
          //new_window.document.write(content_frame_source.replace(new RegExp("<a[^>]*>", "g"), "").replace(new RegExp("</a>", "g"), ""));
          
          // I've tried the following line to no avail
          //new_window.document.write(content_frame_source.replace(/<a[^>]*>/g, "").replace(/<\/a>/g, ""));
    
          // Using this line instead of replacing the links works, but the links are still active (*not* what I want)
          new_window.document.write(content_frame_source);
          
          new_window.document.write("</body></html>");
          new_window.document.close();
        }
      }
    </script>
    </head>
    
    <body>
    <p><a href="#" title="Print" onclick="print(); return false;">Print</a></p>
    <div id="cell_01">
      <iframe id="content_frame" name="content_frame" scrolling="auto" frameborder="0" src="<%=destination%>" title="main content frame">
        <p><a href="<%=destination%>">View the content of this inline frame.</a></p>
      </iframe>
    </div>
    </body>
    </html>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    new_window.document.write(content_frame_source.replace(/<[\/]*a[^>]*>/ig, ""));
    IE converts tag names to uppercase
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    May 2009
    Posts
    10
    Hi Fang,

    Thanks for the reply. I understand where you're going with the case-insensitive match, but I'm still getting a blank window that won't close (or at least won't close without some extra effort - "this program is not responding", "send error report", etc.) when using IE.

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    If I give content_frame a src, it opens in a new window in IE with a print dialog. No problem closing.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    May 2009
    Posts
    10
    Ah... that helps me narrow it down a bit. I replaced the "destination" with a simple test page, and that indeed did work.

    Thanks for the tip, now it appears the issue is with the destination page (inner frame)!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles