Link to home
Start Free TrialLog in
Avatar of Newzone
Newzone

asked on

Javascript Question

I have a new document window opening up from a link. I want the title bar to be a generic statement. I have the code below, but it's not working. Can anyone tell me why?
Thanks in advance!
Michelle

<script type="text/javascript" language="JavaScript">


var win = null;
function newWindow(fstrpage,fstrname,w,h,features)  {
  var b = "You are in the FSTR Website.";
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
   
  win = window.open(fstrpage,fstrname,settings);
  win.document.title = b;
  win.window.focus();
 
}

</script>
Avatar of xchenxchen
xchenxchen

I'v done it already. I'v tried on my IE5.5 and it works well. See the following x.html and xx.html. x.html opens xx.html. Put them in the same directory and load x.html. As you see xx.html is loaded with your own title.

---- x.html ----
<html>
<head>
<script type="text/javascript" language="JavaScript">
var win = null;
function newWindow(fstrpage,fstrname,w,h,features)  {
 var b = "You are in the FSTR Website.";
 var winl = (screen.width-w)/2;
 var wint = (screen.height-h)/2;
 if (winl < 0) winl = 0;
 if (wint < 0) wint = 0;
 var settings = 'height=' + h + ',';
 settings += 'width=' + w + ',';
 settings += 'top=' + wint + ',';
 settings += 'left=' + winl + ',';
 settings += features;
 
 win = window.open(fstrpage,fstrname,settings);
 win.document.title = b;
 win.window.focus();
}
newWindow('xx.html','xx',300,300,'');
</script>
</head>
<body>
</body>
</html>

---- xx.html ----
<html><head>
<title>XXXXXX</title>
</head>
<body>
HI
</body>
</html>
Correction on my previous comment. I really meant "YOU'v done it already" in the first sentence.
Avatar of Newzone

ASKER

Hi xchenxchen,
I tried what you gave me and it opens a third window. I think I didn't explain my project very well. I have this window open from a link and it brings in a pdf file (which may be why I can't change the title bar.) The text above is what I have on the page where all the links are and I've added below how I call the document and new window.

<a href="#null" onClick="newWindow('ace.pdf','','750','550','resizable,scrollbars')">

What do you think?
I get it now. In this case you may not be able to do it as the control is taken over by the Adobe plug-in. You may seek for better answer.
Hi Michelle,

here's a workaround. i've set the script to open another page (test.html in my example) which contains an iframe that occupies the whole page. then i modified newWindow function and sets the source of the iframe to the pdfpage parameter. here is the new script,

opener.html
------------
<html>
<head>
<script type="text/javascript" language="JavaScript">
var win = null;
function newWindow(fstrpage,fstrname,pdfpage,w,h,features)  {
  var b = "You are in the FSTR Website.";
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
 
  win = window.open(fstrpage,fstrname,settings);
  win.document.title = b;
  win.document.getElementById('myframe').src = pdfpage;
  win.window.focus();
}
newWindow('test.html','xx','recommendation_form.pdf',300,300,'');
</script>
</head>
<body>
</body>
</html>


test.html
------------
<html>
<head>
<title>your title</title>
</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<iframe id="myframe" width="100%" height="100%" src="about:blank"></iframe>
</body>
</html>

this is better it uses only your main page. the child window is written on the fly

<html>
<head>
<script type="text/javascript" language="JavaScript">
var win = null;
function newWindow(fstrpage,fstrname,pdfpage,w,h,features)  {
  var b = "You are in the FSTR Website.";
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
 
  win = window.open(fstrpage,fstrname,settings);
  win.document.write('<html><head><title>'+b+'</title></head><body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">');
  win.document.write('<iframe width="100%" height="100%" src="'+pdfpage+'"></iframe>');
  win.document.write('</body></html>');
  win.document.close();
  win.focus();
//  win.document.title = b;
//  win.document.getElementById('myframe').src = pdfpage;
//  win.document.myframe.src = pdfpage;
//  win.window.focus();
}
newWindow('test.html','xx','recommendation_form.pdf',300,300,'');
</script>
</head>
<body>
</body>
</html>

those in comments are from the old code above. you can delete those later.
hmmm, i haven't removed unnecessary codes. here is the final script,

<html>
<head>
<script type="text/javascript" language="JavaScript">
function newWindow(pdfpage,w,h,features)  {
  var b = "You are in the FSTR Website.";
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
 
  win = window.open('','win',settings);
  win.document.write('<html><head><title>'+b+'</title></head><body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">');
  win.document.write('<iframe width="100%" height="100%" src="'+pdfpage+'"></iframe>');
  win.document.write('</body></html>');
  win.document.close();
  win.focus();
}
//newWindow('yourpdf.pdf',300,300,'');
</script>
</head>
<body>
<a href="yourpdf.pdf" onclick="newWindow(this.href,300,300,'');return false">yourpdf</a>
</body>
</html>
Avatar of Newzone

ASKER

Wow, that just about did it. Two things, how do I get it to open back in the left edge (it opens a full window that starts on about the middle of the screen.) I didn't notice that you changed anything in the setting code so I don't know why it's doing that now. And what is "iframe" I haven't seen that before. I had the idea I needed something like that, but I didn't really know how to look for it.  Thanks a lot!
ASKER CERTIFIED SOLUTION
Avatar of third
third
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Newzone

ASKER

Thanks a million!