Solved

Some javascript

Posted on 2011-09-17
5
244 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now