views:

253

answers:

4

A Python script at Google App Engine fetches data into a HTML page.

What is the best way to open a new browser window from the script or HTML page?

JavaScript doesn't work.

+3  A: 

The only way to automatically open a new window in a browser is JavaScript (using window.open()).

If you can't use JavaScript, you can simply add a link to your html page with a _blank target that will open a new window (or sometimes a new tab if the user has configured this browser to do so) :

<a href="newPage.html" target="_blank">Link text</a>

This won't be automatic, ie the user will have to manually click on the link, whereas you could have the window opened without user interaction using javascript (but be aware that window.open() is sometimes blocked by popup blockers).

Wookai
I prefer this way, it's easier on the eyes and more compatible at the same time
slf
You seem to be contradicting yourself.
Justin Johnson
Only javascript can open a window *automatically*, but it can be done differently if we required some user intercation.
Wookai
A: 

I tried for testing purposes to insert this code:

<SCRIPT LANGUAGE="JavaScript">

function openindex()
      {
OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars="+scroll+",menubar=no");
OpenWindow.document.write("<TITLE>Title Goes Here</TITLE>")
OpenWindow.document.write("<BODY BGCOLOR=pink>")
OpenWindow.document.write("<h1>Hello!</h1>")
OpenWindow.document.write("This text will appear in the window!")
OpenWindow.document.write("</BODY>")
OpenWindow.document.write("</HTML>")

OpenWindow.document.close()
self.name="main"
     }
</SCRIPT>

The code is stolen from here: http://www.htmlgoodies.com/beyond/javascript/article.php/3471221

The whole code including the SCRIPT tags was inside the HTML-Page but no new browser window was created.

I tried with Opera and Firefox and I have no popup blocker installed.

Neverland
A: 

Regarding the above code posted by Neverland: the code that you stole simply defines a JavaScript function. The function still needs to be called somewhere in order to run. You could put it in the onLoad handler of the BODY tag or in an onClick handler of a button or link.

The answer to your question has nothing to do with Python or AppEngine. This is basic JavaScript programming.

Adam Crossland
A: 

This code did work, maybe you need call the function, or checkout the contents settings tab, enabling the javascript checkbox (in firefox)

<SCRIPT LANGUAGE="JavaScript">
function openindex(){
  OpenWindow=window.open("", "newwin", "height=250,width=250,toolbar=no,scrollbars="+scroll+",menubar=no");
  OpenWindow.document.write("<TITLE>Title Goes Here</TITLE>")
  OpenWindow.document.write("<BODY BGCOLOR=pink>")
  OpenWindow.document.write("<h1>Hello!</h1>")
  OpenWindow.document.write("This text will appear in the window!")
  OpenWindow.document.write("</BODY>")
  OpenWindow.document.write("</HTML>")

  OpenWindow.document.close()
  self.name="main"
}
 </SCRIPT>
 <button onclick="openindex()">open window</button>
Kristian Damian
100% ACK. I forgot the onLoad="function_name()" inside the <BODY>-Tag.
Neverland