|  | How could I redirect document.write to a new page/tab? |  | |
| | | Mateusz Viste |  |
| Posted: Sat Sep 06, 2008 10:33 am Post subject: How could I redirect document.write to a new page/tab? |  |
| |  | |
Hi,
I am trying make some multimedia files playable from my website. So far, I am able to generate dynamically a new page containing the right <embed> section. However, when I load my script, it overwrites the current page. Is there any way I could load a new tab or window, and put the new content into it?
Here is the script I am using right now:
<script language="JavaScript"><!-- function playmedia() { newwindow=window.open(); if (window.focus) {newwindow.focus()} document.write('<html>') document.write(' <head>') document.write(' <title>Media player</title>') document.write(' </head>') document.write(' <body bgcolor="#A0A0FF" text="#000000">') document.write(' <center>') document.write(' <h2>Playing the media file... ♫</h2><br>') document.write(' <!-- show play buttons, autostart and loop once -->') document.write(' <embed src="title.mid" hidden=false autostart=true loop=1 autosize=1>') document.write(' </center>') document.write(' </body>') document.write('</html>') } //--></script>
Any ideas? :-)
Best regards, Mateusz Viste |
| |
| | | SAM |  |
| Posted: Sat Sep 06, 2008 12:55 pm Post subject: Re: How could I redirect document.write to a new page/tab? |  |
| |  | |
Mateusz Viste a crit :
| Quote: | Hi,
I am trying make some multimedia files playable from my website. So far, I am able to generate dynamically a new page containing the right <embed section. However, when I load my script, it overwrites the current page. Is there any way I could load a new tab or window, and put the new content into it?
Here is the script I am using right now:
script language="JavaScript"><!--
|
No ! no more used, prefer :
<script type="text/javascript">
| Quote: | function playmedia() { newwindow=window.open(); if (window.focus) {newwindow.focus()}
|
// prefer isnstead :
if (typeof newwindow == 'undefined' || newwindow.closed) newwindow = window.open()
// then, because you need to write in the document // of the window 'newwindow' :
var document = newwindow.document; document.open();
| Quote: | document.write('<html>') document.write(' <head>') document.write(' <title>Media player</title>') document.write(' </head>') document.write(' <body bgcolor="#A0A0FF" text="#000000">') document.write(' <center>') document.write(' <h2>Playing the media file... ♫</h2><br>') document.write(' <!-- show play buttons, autostart and loop once -->') document.write(' <embed src="title.mid" hidden=false autostart=true loop=1 autosize=1>') document.write(' </center>') document.write(' </body>') document.write('</html>')
|
document.close(); newwindow.focus();
</script>
| Quote: | Any ideas? 
|
in your code 'document' is the document of the window which has the script and not the document of your new window.
-- sm |
| |
| | | Mateusz Viste |  |
| Posted: Sat Sep 06, 2008 2:26 pm Post subject: Re: How could I redirect document.write to a new page/tab? |  |
В Суббота 06 сентября 2008 16:55, SAM писал:
| Quote: | in your code 'document' is the document of the window which has the script and not the document of your new window.
|
Hi,
Thank you a lot for your comments! About the main issue: indeed, you are right I just replaced "document.write" by "newwindow.document.write", and it works fine :-)
I would have a secondary question (still about the same script). I am triggering my script using a button: <form><input type="button" value="Play" onClick="playmedia()"></form>
Is there any way I could do that using a link?
Best regards, Mateusz Viste |
| |
| | | Mateusz Viste |  |
| Posted: Sat Sep 06, 2008 4:30 pm Post subject: Re: How could I redirect document.write to a new page/tab? |  |
В Суббота 06 сентября 2008 19:42, uzumaki.naruto.is@gmail.com писал:
| Quote: | form action="#" id="frm" div id="dummy" a href="#" onclick="playmedia();">Play</a /div /form
Also make sure your documents are validated against the DOCTYPE you are using. "action" is a mandatory attribute of the <form> tag.
|
Great, all is working now  Thank you, guys!
Best regards, Mateusz Viste |
| |
| | | SAM |  |
| Posted: Sat Sep 06, 2008 4:39 pm Post subject: Re: How could I redirect document.write to a new page/tab? |  |
| |  | |
Mateusz Viste a écrit :
| Quote: | В Суббота 06 сентября 2008 16:55, SAM писал: in your code 'document' is the document of the window which has the script and not the document of your new window.
Hi,
Thank you a lot for your comments! About the main issue: indeed, you are right I just replaced "document.write" by "newwindow.document.write", and it works fine 
|
Don't forget to add at end of your writting :
newwindow.document.close();
If not, my Fx never stops to wait the end of document.
| Quote: | I would have a secondary question (still about the same script). I am triggering my script using a button: form><input type="button" value="Play" onClick="playmedia()"></form
|
<form action="javascript:playmedia()"> <input type="submit" value="Play"> </form>
| Quote: | Is there any way I could do that using a link?
|
<a href="errorJS.htm" onclick="playmedia(); return false;">Play</a>
To play sound with 'embed' in same page (same window) :
<html> <script type="text/javascript"> function playmedia(title) { var E = document.createElement('embed'); E.src = title.href; E.hidden = false; E.controller = true; E.autostart = true; E.loop = 1; E.autosize = 1; if(document.getElementById('player')) document.getElementById('player').parentNode.replaceChild( E, document.getElementById('player')); else document.body.appendChild(E); E.id = 'player'; return false; } </script> <h2>Play sounds</h2> <ul> <li><a href="sound1.mid" target="play" onclick="return playmedia(this)">sound 1</a></li> <li><a href="sound2.mid" target="play" onclick="return playmedia(this)">sound 2</a></li> <li><a href="sound3.mid" target="play" onclick="return playmedia(this)">sound 3</a></li> </ul> </html>
Embed it deprecated !
-- sm |
| |
| | | Guest |  |
| Posted: Sat Sep 06, 2008 5:42 pm Post subject: Re: How could I redirect document.write to a new page/tab? |  |
| |  | |
On Sep 6, 9:26pm, Mateusz Viste <mateusz.visteATmailDO...@trashymail.com> wrote:
| Quote: | 06 2008 16:55, SAM :
in your code 'document' is the document of the window which has the script and not the document of your new window.
Hi,
Thank you a lot for your comments! About the main issue: indeed, you are right I just replaced "document.write" by "newwindow.document.write", and it works fine :-)
I would have a secondary question (still about the same script). I am triggering my script using a button: form><input type="button" value="Play" onClick="playmedia()"></form
Is there any way I could do that using a link?
Best regards, Mateusz Viste
|
<form action="#" id="frm"> <div id="dummy"> <a href="#" onclick="playmedia();">Play</a> </div> </form>
Also make sure your documents are validated against the DOCTYPE you are using. "action" is a mandatory attribute of the <form> tag. Make sure the HTML event handler attributes are all lowercase i.e. onclick instead of onClick. The comp.lang.javascript FAQ is a must read. < LINK >
Regards, /sasuke |
| |
| | | Mateusz Viste |  |
| Posted: Sat Sep 06, 2008 6:00 pm Post subject: Re: How could I redirect document.write to a new page/tab? |  |
В Суббота 06 сентября 2008 20:39, SAM писал:
| Quote: | Is there any way I could do that using a link?
a href="errorJS.htm" onclick="playmedia(); return false;">Play</a
|
So far, so good  I had some formatting troubles using FORM, but with the method above it works just fine :-)
My scripting is finalized now. I needed that stuff for LINK (and all sub-pages)
Again, thanks guys ;-)
Best regards, Mateusz Viste |
| |
| | | Dr J R Stockton |  |
| Posted: Sat Sep 06, 2008 8:42 pm Post subject: Re: How could I redirect document.write to a new page/tab? |  |
| |  | |
In comp.lang.javascript message <48c2789c$0$1175$426a34cc@news.free.fr>, Sat, 6 Sep 2008 14:33:32, Mateusz Viste <mateusz.visteATmailDOTru@trashy mail.com> posted:
| Quote: | I am trying make some multimedia files playable from my website. So far, I am able to generate dynamically a new page containing the right <embed section. However, when I load my script, it overwrites the current page. Is there any way I could load a new tab or window, and put the new content into it?
|
See NewPage and/or FreshPage in the include1.js section of <URL:http://w ww.merlyn.demon.co.uk/js-nclds.htm>, with the routines they call.
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
-- (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3 news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>. <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links. |
| |
| | | RobG |  |
| Posted: Sun Sep 07, 2008 2:55 am Post subject: Re: How could I redirect document.write to a new page/tab? |  |
| |  | |
On Sep 7, 12:55am, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid> wrote:
| Quote: | Mateusz Viste a crit : [...] |
| Quote: | document.write('<html>') document.write(' <head>') document.write(' <title>Media player</title>') document.write(' </head>') document.write(' <body bgcolor="#A0A0FF" text="#000000">') document.write(' <center>') document.write(' <h2>Playing the media file... ♫</h2><br>') document.write(' <!-- show play buttons, autostart and loop once -->') document.write(' <embed src="title.mid" hidden=false autostart=true loop=1 autosize=1>') document.write(' </center>') document.write(' </body>') document.write('</html>')
|
It is more efficient to create a single string and use document.write once rather than make several calls. The center element is deprecated, use a suitably styled div element. It doesn't seem sensible to write comments.
e.g.
document.write( '<html><head><title>Media player</title>' + '</head><body bgcolor="#A0A0FF" text="#000000">' + '<div style="text-align: center;">' + '<h2>Playing the media file... ♫</h2><br>' + '<embed src="title.mid" hidden=false autostart=true' + ' loop=1 autosize=1></div></body></html>' );
-- Rob |
| |
|
|