How could I redirect document.write to a new page/tab?

M

Mateusz Viste

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
 
S

SAM

Mateusz Viste a écrit :
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 :

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();
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();

}

Any ideas? :)

in your code 'document' is the document of the window which has the
script and not the document of your new window.
 
M

Mateusz Viste

Ð’ Суббота 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
 
U

uzumaki.naruto.is

÷ óÕÂÂÏÔÁ 06 ÓÅÎÔÑÂÒÑ 2008 16:55, SAM ÐÉÓÁÌ:


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.
< http://www.jibbering.com/faq/ >

Regards,
/sasuke
 
M

Mateusz Viste

Ð’ Суббота 06 ÑентÑÐ±Ñ€Ñ 2008 19:42, (e-mail address removed) пиÑал:
<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
 
S

SAM

Mateusz Viste a écrit :
Ð’ Суббота 06 ÑентÑÐ±Ñ€Ñ 2008 16:55, SAM пиÑал:

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.
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()">
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 !
 
M

Mateusz Viste

Ð’ Суббота 06 ÑентÑÐ±Ñ€Ñ 2008 20:39, SAM пиÑал:
<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
http://viste.homeip.net/mateusz/nes/htm_roms/smb3.htm (and all sub-pages)

Again,
thanks guys ;-)

Best regards,
Mateusz Viste
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Sat, 6 Sep 2008 14:33:32, Mateusz Viste <mateusz.visteATmailDOTru@trashy
mail.com> posted:
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.
 
R

RobG

Mateusz Viste a écrit : [...]
  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>'
);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top