?
Solved

Problem with writeln in a popup using internet explorer.

Posted on 2009-12-21
7
Medium Priority
?
524 Views
Last Modified: 2013-12-08
Hi
I am trying to make a remote javascript file run a locally defined function at runtime. This local function simply writes out everything that is passed into it with writeln.

local.htm creates a popupwindow:

if((win = window.open('',popupname,popuparg)) == null || (doc = win.document.open('text/html', 'replace')) == null) {
    alert('error');
  }
  else {
    doc.writeln('<html><head>');
    doc.writeln('<script language=javascript><!--');
    doc.writeln('function localfunction(text) { document.writeln(text); }');
    doc.writeln('window.focus();');
    doc.writeln('//--></'+'script>');
    doc.writeln('</'+'head><body></'+'body>');
    doc.writeln('<script language=javascript><!--');
    doc.writeln('var sobj = document.createElement("script");');
    doc.writeln('sobj.setAttribute("type", "text/javascript");');
    doc.writeln('sobj.setAttribute("charset", "iso-8859-1");');
    doc.writeln('sobj.setAttribute("src","http://remotesite.com/script.js");');
    doc.writeln('var hobj = document.getElementsByTagName("head").item(0);');
    doc.writeln('hobj.appendChild(sobj);');
    doc.writeln('//--></'+'script>');
    doc.writeln('</'+'html>');  
    doc.close();  
  }

This generate a popup with the local function and an include of the remote script. The remote script contains:
localfunction('Hello');

My wish is that 'Hello' will be printed in the popup window but it won't (in IE). However if I include a simple alert() in the popup after the remotescript has been included everything works fine and 'Hello' is being displayed.

Any ideas?
0
Comment
Question by:niklasmo
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26095765
Your code does  not give IE the chance to open the window

I have removed the dangerous and unnecessary comment tags

var tId = "";
if((win = window.open('',popupname,popuparg)) == null || (doc = win.document.open('text/html', 'replace')) == null) {
    alert('error');
}
else {
  tId=setTimeout('writePage()',300);
} 
function writePage() {
    var text = "";
    text += '<html><head>';
    text += '<script language=javascript>';
    text += 'function localfunction(text) { document.writeln(text); }';
    text += 'window.focus();';
    text += '<\/script>';
    text += '</'+'head><body></'+'body>';
    text += '<script language=javascript>');
    text += 'var sobj = document.createElement("script");';
    text += 'sobj.setAttribute("type", "text/javascript");';
    text += 'sobj.setAttribute("charset", "iso-8859-1");';
    text += 'sobj.setAttribute("src","http://remotesite.com/script.js");';
    text += 'var hobj = document.getElementsByTagName("head").item(0);';
    text += 'hobj.appendChild(sobj);';
    text += '<\/script>';
    text += '</'+'html>';  
    doc.writeln(text);
    doc.close();  
}

Open in new window

0
 

Author Comment

by:niklasmo
ID: 26095835
The code is enclosed in a function that is called by clicking a link.

IE opens up the window but doesn't fill it with what I expect i.e. the string 'Hello' in my example above.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26096644
Erm sorry. It is the win.document.open we need to delay.
here is how I would do it - but a div would be more clever

<html>
<head>
<script type="text/javascript">
var tId = "";
var win;
funtion popWin(popupname,popuparg) {
  win = window.open('',popupname,popuparg);
  tId=setTimeout('writePage()',300);
  return false;
} 
function writePage() {
    if (!win) {
      alert('Error');
      return;
    }
	
    var doc = win.document.open('text/html', 'replace')
    var text = "";
    text += '<html><head>';
    text += '<script language=javascript>';
    text += 'function localfunction(text) { document.writeln(text); }';
    text += 'window.focus();';
    text += '<\/script>';
    text += '</'+'head><body></'+'body>';
    text += '<script language=javascript>');
    text += 'var sobj = document.createElement("script");';
    text += 'sobj.setAttribute("type", "text/javascript");';
    text += 'sobj.setAttribute("charset", "iso-8859-1");';
    text += 'sobj.setAttribute("src","http://remotesite.com/script.js");';
    text += 'var hobj = document.getElementsByTagName("head")[0];';
    text += 'hobj.appendChild(sobj);';
    text += '<\/script>';
    text += '</'+'html>';  
    doc.writeln(text);
    doc.close();  
}
</script>
</head>
<body>
<a href="#" onClick="return popWin('win1','width=400,height=500')">Pop</a>
</body>
</html>

Open in new window

0
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 

Author Comment

by:niklasmo
ID: 26171091
I'm sorry for my late reply due to the holidays and all.

I tried the solution above with no progress.

It's like the included script will not run until the alert() function is run. Why? It certainly feels lika a timing issue (the code work flawlessly in Firefox)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26171334
NOW I see what you mean.
I fixed some bugs in my script and then created this version which can be used to show your issue.
If I remove the alert, the text is not written

<html>
<head>
<script type="text/javascript">
var tId = "";
var win = null;
function popWin(popupname,popuparg) {
  win = window.open('',popupname,popuparg);
  tId=setTimeout('writePage()',300);
  return false;
} 
function writePage() {
    if (!win) {
      alert('Error');
      return;
    }
        
    var doc = win.document.open('text/html', 'replace');
    var text = "";
    text += '<html><head>';
    text += '<script language=javascript>';
    text += 'function localfunction(text) { document.writeln(text); }';
    text += 'function version(num) { alert(num); document.writeln(num); }';
    text += 'window.focus();';
    text += '<\/script>';
    text += '</head><body></body>';
    text += '<script language=javascript>';
    text += 'var sobj = document.createElement("script");';
    text += 'sobj.setAttribute("type", "text/javascript");';
    text += 'sobj.setAttribute("charset", "iso-8859-1");';
    text += 'sobj.setAttribute("src","http://mxr.mozilla.org/mozilla/source/js/tests/js1_1/jsref.js?raw=1");';
    text += 'var hobj = document.getElementsByTagName("head")[0];';
    text += 'hobj.appendChild(sobj);';
    text += '<\/script>';
    text += '</html>';  
    doc.writeln(text);
    doc.close();  
}
</script>
</head>
<body>
<a href="#" onClick="return popWin('win1','width=400,height=500')">Pop</a>
</body>
</html>

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 26171369
This works

<html>
<head>
<script type="text/javascript">
var tId = "";
var win = null;
function popWin(popupname,popuparg) {
  win = window.open('',popupname,popuparg);
  tId=setTimeout('writePage()',300);
  return false;
} 
function writePage() {
    if (!win) {
      alert('Error');
      return;
    }
        
    var doc = win.document.open('text/html', 'replace');
    var text = "";
    text += '<html><head>';
    text += '<script language=javascript>';
    text += 'function localfunction(text) { document.writeln(text); }';
    text += 'function version(num) { document.getElementById("content").innerHTML=num; }';
    text += 'window.focus();';
    text += '<\/script>';
    text += '</head><body><div id="content"></div></body>';
    text += '<script language=javascript>';
    text += 'var sobj = document.createElement("script");';
    text += 'sobj.setAttribute("type", "text/javascript");';
    text += 'sobj.setAttribute("charset", "iso-8859-1");';
    text += 'sobj.setAttribute("src","http://mxr.mozilla.org/mozilla/source/js/tests/js1_1/jsref.js?raw=1");';
    text += 'var hobj = document.getElementsByTagName("head")[0];';
    text += 'hobj.appendChild(sobj);';
    text += '<\/script>';
    text += '</html>';  
    doc.writeln(text);
    doc.close();  
}
</script>
</head>
<body>
<a href="#" onClick="return popWin('win1','width=400,height=500')">Pop</a>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:niklasmo
ID: 31668484
Thank you very much for your quick and accurate response! This works for me as well.

(Can't believe I didn't try that earlier though.)

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question