1

I am trying to write JavaScript code that will execute once the child page (created with window.open function) is loaded completely.

I tried functions like window.onload and window.setTimeout but none seem to work. I tried to use different browsers too, but no luck.

I have a HTML file, myhtml.html, which calls an external JavaScript file, Test1.js. Test1.js opens a webpage (www.google.com) then generates a popup.

HTML:

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Testin page</title>
  <script type="text/javascript" src="Test1.js"></script>
  <script type="text/javascript">
    function  callValidation()
    {
      var p;
      var i=1;

      if(document.getElementById("radio1").checked){
          p="radio1";
      }
      directValidation(p);
      alert("Ending script");
    }
  </script>
</head>
<body>
  <h1><em><strong>Test Page</strong></em></h1>
  <h4><em><strong>Version 1.0</strong></em></h4>
  <form name="mainf" id = "mainf">
    <input id="radio1" name ="radio1" type="radio" value = "Test Button"/>
    <input type = "submit" name="submit" OnClick="callValidation();"/>
  </form>
</body>
</html>

Test1.js:

var p;
function directValidation(p)
{
  alert(p);

  if (p="radio1")
  {
    fun1();
  }
}

function fun1()
{
  window.open("http://www.google.com");
  window.onload = function(){ alert('Not Working!!'); }
  alert("It is working but without waiting for page to load!! The above alert box didn't work");
}

In the above code, alert('Not Working!!');, is not executing.

alert("It is working but without waiting for page to load!! The above alert box didn't work");

is executing before loading www.google.com.

In the above code I even tried the method: window.setTimeout('',5000); but I'm not able to get a 5-second delay with this either.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Aditya
  • 83
  • 1
  • 2
  • 11
  • Could you confirm that your popup with Google opens, and you have the alert with "It is working..." ? – Niko May 07 '15 at 14:00
  • Alert with "It is working..." is working perfectly but it is popping up before www.google.com is loaded completely. – Aditya May 07 '15 at 14:02
  • See [this answer](http://stackoverflow.com/questions/2797560/set-a-callback-function-to-a-new-window-in-javascript) which has a solution. – Robin May 07 '15 at 14:02
  • [window.open](https://developer.mozilla.org/en-US/docs/Web/API/Window/open) return reference to opened window, but in your case you add load handler to current window – Grundy May 07 '15 at 14:03
  • Robin: I edited the script as you suggested in that post w.onload = function(){ alert('Not Working!!'); } alert("It is working but without waiting for page to load!! The above alert box didnt work"); Still no luck – Aditya May 07 '15 at 14:11
  • Can you clarify when exactly and which case is "It is working but ..." supposed to be displayed ? because here it will always be shown – Niko May 07 '15 at 14:20
  • alert box with "Not working" is never executing. and the alert box with "It is working..." is generating before www.google.com is loaded. I want it to be run once page is loaded. (In short i want every code after window.open to execute after Google page is open and fully loaded) – Aditya May 07 '15 at 14:24

4 Answers4

1

window.open will open its own window, with its own environment. If you want a popup displayed in the new window, you'll need to have that in the source code for the page you load in the new window.

In your line

window.onload = function(){ alert('Not Working!!'); }

the window object is the SOURCE window, not the NEWLY OPENED window. The source window has likely long passed the onload event.

Edit

If you just want to open a query to google in a new tab, try:

window.open("http://www.google.com/#q=SEARCH_TERM");

but be warned - unless this is executed as a direct result of a user interaction (eg: clicking a button) then this will usually just be blocked by the browser as it is considered a "pop-up".

Alex McMillan
  • 17,096
  • 12
  • 55
  • 88
  • then how to direct the call to child window?? and check if child window is loaded properly? – Aditya May 07 '15 at 14:07
  • When you load the child window, you'll give it a URL to load. Serve some javascript up at that URL. – Alex McMillan May 07 '15 at 14:10
  • sorry can you elaborate a little further as am quite new in java scripting. – Aditya May 07 '15 at 14:18
  • If you are running code on PAGE A, you can open a new window to PAGE B - but that page is owned by whatever code is run there. You can't run code on PAGE A and have it affect PAGE B in a different window/tab. – Alex McMillan May 07 '15 at 14:18
  • For example, if `www.google.com` runs some javascript that opens a new tab to `www.facebook.com`, the code running on Google can't do things on the Facebook tab. – Alex McMillan May 07 '15 at 14:20
  • Then how to make changes to the child page? I earlier automated using vb script and there it appears to be possible to access elements of child elements. I am surprised it if is not possible using java script. – Aditya May 07 '15 at 14:31
  • There is no such thing as a "child page" (unless you're talking about an [iFrame](http://www.w3schools.com/tags/tag_iframe.asp)). What you're asking is akin to opening a door in your own house and expecting to move furniture in your neighbours bedroom. – Alex McMillan May 07 '15 at 14:36
  • Thanks Alex for promt reply. I didnt want to increase complexity but i guess that created more complication. What i basically want is to make a script to open www.google.com and in that enter a keyword, "Books by JK Rolling" and then press search button on google page. Somewhat like automation of google search.. – Aditya May 07 '15 at 15:08
  • Thanks but I am looking for more generic way using: document.getElementByID("button to be clicked").click() AS yours will run only for google or some specific search engine.. I am looking to find on multiple search engine without limiting that if search engine support search using address bar. – Aditya May 08 '15 at 10:47
  • Basically I am using it for automating some manual task and searching is just step 1 of it. – Aditya May 08 '15 at 10:48
  • I don't think you can do what you want using Javascript. – Alex McMillan May 08 '15 at 10:52
  • Finally I shifted to power-shell for automation :P – Aditya May 19 '15 at 11:01
0

Try to put onload outside function. That is event and it's fire when event occurred, in this case when windows is loaded. On way you done this, when you called fun1 you bind onload event to window.

lobuljen
  • 109
  • 1
  • 12
0

When you do

window.onload = function(){ alert('Not Working!!'); }

In your code, window refers to your main page.

Since you want it to be related to the newly opened page, you have to get the reference of the new page, which is returned by window.open()

var openedWindow = window.open("http://www.google.com");
openedWindow.onload = showAlert();

function showAlert() {
    alert('Not Working!!');
}
Niko
  • 3,412
  • 26
  • 35
  • edited code: var w=window.open("http://www.google.com"); w.onload = function(){ alert('Not Working!!'); } alert("It is working but without waiting for page to load!! The above alert box didnt work"); Still no change... Am I doing wrong here?? – Aditya May 07 '15 at 14:13
  • I have changed the onload part, now the alert is displayed correctly when the popup is loading – Niko May 07 '15 at 14:39
  • Thanks Niko. But looks like still no luck. Did you tried it on you machine? If yes can tell which browser you are using?? – Aditya May 07 '15 at 14:46
  • Just edited again for a more proper way, works on chrome try last version of code ;) – Niko May 07 '15 at 14:47
  • Yes is showing "Not Working" popup but unfortunately it is not waiting for www.google.com to load completely. It is just popping as soon as i press submit button – Aditya May 07 '15 at 14:51
0

In the page that is supposed to open, in the body tag, just put a onload tag :

<body onload="yourfunction()">HTML code</body>
ninivert
  • 45
  • 9
  • It is google page can you suggest some link or methodology to put onload in it?? I dont want to use document.write method to edit any html code of child window. – Aditya May 07 '15 at 14:17
  • Can't you use an eventlistener to detect when the page is loaded. Never got a chance to play around with that though, you'll have to figure that out on your own :( – ninivert May 07 '15 at 14:43
  • Tried but its not referring to www.google.com instead myhtml.html – Aditya May 07 '15 at 14:47