Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Some javascript

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

636 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