Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1054839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:28:08+00:00 2026-05-16T17:28:08+00:00

When you execute following example using Firefox 3: <html> <head> <script language=javascript type=text/javascript> <!–

  • 0

When you execute following example using Firefox 3:

<html>
 <head>
  <script language="javascript" type="text/javascript">
   <!--
   function openWindow(){
    var w = window.open('', 'otherWin', 'width=600,height=600');
    w.document.write(document.getElementsByTagName("html")[0].innerHTML);
    w.document.close();
    reportLinks(w.document.links);
   }

   function reportLinks(links){
    var report = 'links: '+links.length;
    for (var i=0;i<links.length;i++){
     report += '\n (link='+links[i].href+')';
    }
    alert(report);
   }
  //-->
  </script>
 </head>
 <body>
  <p><a href="javascript: openWindow()">Open Same Content and Show Links Report</a></p>
  <p><a href="javascript: reportLinks(document.links)">Show Links Report</a></p>
 </body>
</html>

You will see that both the number of links shown when clicking on ‘Show Links Report’ as when clicking on ‘Open Same Content and Show Links Report’ will be 2. However when having an external JavaScript file reference from this page the behavior seems different (just make an empty file some.js if you want). When clicking ‘Open Same Content and Show Links Report’ the number of links will be 0.

<html>
 <head>
  <script language="javascript" type="text/javascript" src="some.js"></script>
  <script language="javascript" type="text/javascript">
   <!--
   function openWindow(){
    var w = window.open('', 'otherWin', 'width=600,height=600');
    w.document.write(document.getElementsByTagName("html")[0].innerHTML);
    w.document.close();
    reportLinks(w.document.links);
   }

   function reportLinks(links){
    var report = 'links: '+links.length;
    for (var i=0;i<links.length;i++){
     report += '\n (link='+links[i].href+')';
    }
    alert(report);
   }
  //-->
  </script>
 </head>
 <body>
  <p><a href="javascript: openWindow()">Open Same Content and Show Links Report</a></p>
  <p><a href="javascript: reportLinks(document.links)">Show Links Report</a></p>
 </body>
</html>

It is probably a matter of loading the page and the moment that reportLinks executed exactly. I assume that the external some.js is added that the document is not completely build up. Is there a way that I can register this reportLinks call for onload event so that I can be sure that document.links is complete?

By the way the example works fine in both cases with Google Chrome.

(added after answer1)

As suggested by Marcel K. I rewrote the example, added also the code the way I really would like to have the thing going. And now testing it, and this simple example seems to work with Firefox and with Chrome.

<html>

 <head>

  <script type="text/javascript" src="some.js"></script>
  <script type="text/javascript">
   <!--
   function openWindow(){
    var w = window.open('', 'otherWin', 'width=600,height=600');
    w.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n'+
       document.getElementsByTagName("html")[0].innerHTML+'\n</html>');
w.onload=function(){
     reportLinks(w.document.links);
    };
    w.document.close();
   }

   function reportLinks(links){
    var report = 'links: '+links.length;
    for (var i=0;i<links.length;i++){
     report += '\n (link='+links[i].href+')';
    }
    alert(report);
   }
  //-->
  </script>
 </head>
 <body>
  <p><a href="javascript: openWindow()">Open Same Content and Show Links Report</a></p>
  <p><a href="javascript: reportLinks(document.links)">Show Links Report</a></p>
 </body>
</html>

I had hoped with this simple example to show a simple case of the actual code I am writing. A print preview screen of complicated html in which I want to disable all hrefs once opened. But in that one the onload handler is never called… How can I register an onload handler in this case in the most robust way?

Many thanks,
Marcel

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:28:08+00:00Added an answer on May 16, 2026 at 5:28 pm

    The only way I could make the example above work portable over Firefox, Chrome and IE is by registering the onload listener through inlined JavaScript in the HTML loaded in the popup window. Following example code shows how.

    <html>
     <head>
      <script type="text/javascript" src="script.js"></script>
     </head>
     <body>
      <p><a href="javascript: openWindow()">Open Same Content and Show Links Report</a></p>
      <p><a href="javascript: reportLinks(document.links)">Show Links Report</a></p>
     </body>
    </html>
    

    This page uses a script in script.js file. Following shows the content of that file.

    function openWindow(){
      var w = window.open('', 'otherWin', 'width=600,height=600');
      w.document.write(
        '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n'+
        document.getElementsByTagName("html")[0].innerHTML+
        '\n <script type="text/javascript">\n'+
        '  function addOnloadListener(listener){\n'+
        '    if (window.addEventListener) {\n'+
        '      window.addEventListener("load", listener, false);\n'+
        '    } else {\n'+
        '      window.attachEvent("onload",listener);\n'+
        '    }\n'+
        '  }\n'+
        '  addOnloadListener(function(){reportLinks(document.links);});\n'+
        ' </script>\n'+
        '</html>');
      w.document.close();
    }
    
    function reportLinks(links){
      var report = 'links: '+links.length;
      for (var i=0;i<links.length;i++){
        report += '\n (link='+links[i].href+')';
      }
      alert(report);
    }
    

    When putting the function addOnloadListener directly in the JavaScript file (not inlined in the page) it doesn’t work in IE6 because, I believe, it cannot handle the order of script entries correctly. When addOnloadListener was not inlined the inlined call to addOnloadListener didn’t work, it simply couldn’t find the function in the earlier:

    <script type="text/javascript" src="script.js"></script>
    

    The code is only a simple example that doesn’t really do a lot. I used it for disabling all links in a print preview popup page.

    A simpler way to register an onload listener for a popup window portable over browser is always welcome.

    Thanks,
    Marcel

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following html page: <html> <head> <title>AJAX Example</title> <meta http-equiv=Content-Type content=text/html;
When using an importjs() type of function (see below for an example), jQuery doesn't
I have the following example page structure: Webpage.aspx Script.aspx If I call Server.Execute(Script.aspx) from
When I execute this following code for example: cart.php?p=1&action=add <?php session_start(); if (isset($_GET['p']) &&
I am wondering is there any way to execute following shell script, which waits
When I execute the following function, I get an error: Error 'Excel1.WebServiceFunctions.CreateMyTask()': not all
What purpose does using serve when used the following way:- ONE EXAMPLE IS THIS,
Javascript containing errors sometimes does not execute without any error message. The following line
i am new to using c++ When I execute the following code i am
I can compile but can't execute the following code with the error (using Postgres):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.