-1

I have this code to open a new window and pass a div's content into a div in the new window:

<script type="text/javascript">
var changeIt, print_div;
changeIt = function()
{ print_div = document.getElementById("sec_content").innerHTML;
newwindow=window.open( 'targetpage.html', target="_blank" );
setTimeout('newwindow.document.getElementById("print_content").innerHTML=print_div',100)
};
</script>

but for some reason it's not working online (it does open a new window but it doesn't pass the sec_content into print_content) , it only does when I use an alert before the setTimeout, like this:

<script type="text/javascript">
var changeIt, print_div;
changeIt = function()
{ print_div = document.getElementById("sec_content").innerHTML;
newwindow=window.open( 'targetpage.html', target="_blank" );
alert("eeeaaa");
setTimeout('newwindow.document.getElementById("print_content").innerHTML=print_div',100)
};
</script>

Can anyone tell me why please, I really need help with this.

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Ariel_medel
0

Ariel,

It looks like it's a question of timing. Your 100ms delay is not enough for the page to load. The alert halts execution of the script and introduces extra delay, allowing targetpage.html to load into the popup window.

The solution is not to rely on a prefdefined delay (of any length). Instead, separate the script into two parts:

  1. Open the window
  2. Transfer the data

There are essentially three strategies for initiating the data transfer:

  1. Pass data in the request. (Tricky unless you know how).
  2. In the main window, poll until "popup-loaded" is detected, then pass the data. (Again tricky unless you know how).
  3. Arrange for targetpage.html to request data transfer as soon as it has loaded.

(3) is by far the easiest.
In the main window

function openWin(){
  newwindow = window.open( 'targetpage.html', target="_blank" );
};
function transferData(targetElement){
  print_div = document.getElementById("sec_content");
  if(print_div){
    targetElement.innerHTML = print_div.innerHTML;
  }
}

In targetpage.html :

onload = function(){
  var el = document.getElementById("print_content");
  if(el){ opener.transferData(el); }
}

I've not tested any of this - it's straight off-the-cuff - so may need debugging.

Anyway, that's the gist of it.

Airshow

Edited by Airshow: n/a

0

I have this code to open a new window and pass a div's content into a div in the new window:

<script type="text/javascript">
var changeIt, print_div;
changeIt = function()
{ print_div = document.getElementById("sec_content").innerHTML;
newwindow=window.open( 'targetpage.html', target="_blank" );
setTimeout('newwindow.document.getElementById("print_content").innerHTML=print_div',100)
};
</script>

but for some reason it's not working online (it does open a new window but it doesn't pass the sec_content into print_content) , it only does when I use an alert before the setTimeout, like this:

<script type="text/javascript">
var changeIt, print_div;
changeIt = function()
{ print_div = document.getElementById("sec_content").innerHTML;
newwindow=window.open( 'targetpage.html', target="_blank" );
alert("eeeaaa");
setTimeout('newwindow.document.getElementById("print_content").innerHTML=print_div',100)
};
</script>

Can anyone tell me why please, I really need help with this.

There is a way to avoid relying on uncertainty of timers and methods like that.

Keep this part of your existing code:

<script type="text/javascript">
var changeIt, print_div;
changeIt = function() {
	print_div = document.getElementById("sec_content").innerHTML;
	newwindow=window.open('targetpage.html', target='_blank');
}
changeIt()
</script>

and add this to your target page:

<script type="text/javascript">
document.getElementById("print_content").innerHTML=opener.print_div
</script>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.