?
Solved

Some javascript

Posted on 2011-09-17
5
Medium Priority
?
251 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

741 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