Solved

Some javascript

Posted on 2011-09-17
5
247 Views
Last Modified: 2012-05-12
Hello,

I need help in writing some JS script..

I have a web application and when it renders a page i also want it to open a second browser window that displays a PDF file (The second window holds help information).

I want the PDF to be displayed to the right of the screen (I can experiment with the cordinates).  Ideally if the browser window is already open it then the script should just replace the contents (and not open a new window).

I would also like some code to close the window...

I knows its the weekend but if anyone could help out that would be great....
0
Comment
Question by:soozh
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:acbxyz
ID: 36554515
Maybe this will help:
var helpwindow;
function dothisonload() {
  helpwindow = window.open("datei.pdf", "Helping window", "width=200,height=200,left=500,top=50");
}

function dothisonuload() {
  if (helpwindow) {
    helpwindow.close;
  }
}

Open in new window


But positioning of windows will depend on browser settings. No big problem on intranets, but very unreliable on internet. Additinally, not all browsers open pdf files inside itself. If the browser offers the file for downloading or opening with an external program, there won't be anything to change this, at least not in js.
0
 

Author Comment

by:soozh
ID: 36555023
Thanks - will this "replace" the pdf document when i open the window again? Or will it open a new window?
0
 
LVL 10

Expert Comment

by:acbxyz
ID: 36555046
If you use window.open again it will open another window. Well, if using the same window name (second argument) I'm not sure, if using another there will be another window.
If you want to replace the content you can use something like:
if (helpwindow) {
  helpwindow.location.href = 'newfile.pdf';
} else {
  helpwindow = window.open('newfile.pdf', 'window name', 'options_see_above');
}

Open in new window

This will first check if the window is still open. If it is, it will redirect its content to the new file. Otherwise a new window is opened.

PS: If you redirect the "main" site or just reload, the variable helpwindow won't direct to the opened window any more. Because of browser security policies this is not easy to avoid.
0
 

Author Comment

by:soozh
ID: 36555617
If seems to me that if the user has closed the window then helpwindow might have a valid window handle or whatever it has but the window itself will no longer be there.  Is that a correct assumption?
0
 
LVL 10

Accepted Solution

by:
acbxyz earned 500 total points
ID: 36555952
You are correct, but this can be checked easy via asking the closed property of your window.
At least firefox doesn't open a new window if the window name already exists. Thus you can update your window content from another or refreshed main page by calling window.open with the same window name.

Here is the complete and tested code:
var helpwindow;
var windowoptions;

function dothisonload() {
    windowoptions = "width=200,height=200,left=500,top=50";
    helpwindow = window.open("exex2.php?say=onload", "Helping_window", windowoptions);
}

function dothisonuload() {
    if (helpwindow && !helpwindow.closed) {
        helpwindow.close();
    }
}

function dothistoredirect() {
    if (helpwindow && !helpwindow.closed) {
        helpwindow.location.href = 'exex2.php?say=newfile';
    } else {
        helpwindow = window.open('exex2.php?say=newwindow', 'Helping_window', windowoptions);
    }
}

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

830 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