Solved

Some javascript

Posted on 2011-09-17
5
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

691 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