View Full Version : how to stop document.write(opener[...] deleting following lines??
Hilary
01-05-2003, 05:21 PM
Hi,
I'm trying to get a 'recommend a friend' cgi script to work in a popup window, using javascript 'opener' to write the url of the recommended page into the form.
So the problem bit of code:
<script language="javascript">
<!--
document.write('<input type="hidden" name="url" value="');
document.write(opener.location.href);
document.write('"> ');
//-->
</script>
(I want this to result in something like
<input type="hidden" name="url" value="http://www.domain.com/page.html">)
I've found that the above js results in a couple of lines after </script> being deleted. But if the word 'opener' is removed, this doesn't happen. I can get it to write in the current url, for instance -
document.write(window.location.href);
- with no problem.
I really hope someone knows how to fix this! If you do, please bear in mind that I know no javascript at all (*blush*), and pasting in code is about my limit.
Thank you very much indeed for any help!
Hilary
scroots
01-05-2003, 05:46 PM
is opener used in another script on your page?
scroots
Hilary
01-05-2003, 06:28 PM
afraid not... (but thanks for the thought)
scroots
01-05-2003, 08:27 PM
is the page on the internet? if it is can you post a link to it so people can look at the source code or post the source code.
scroots
Hilary
01-06-2003, 12:25 AM
I can't quite point you to the original, as the html for the form is actually generated by a cgi script (but I've been testing the html alone, and that behaves in exactly the same way).
Anyway, here's the complete html:
<form action="$ENV{'SCRIPT_NAME'}" method="post" name="mailpage">
<script language="javascript">
<!--
document.write('<input type="hidden" name="url" value="');
document.write(opener.location.href);
document.write('"> ');
//-->
</script>
<input type="hidden" name="action" value="send">
<center>
<table cellpadding="3" cellspacing="0" border="0">
<tr><td align="left" colspan="2" class="shrink">Please fill out this quick form to send the page to your friend.
<br></td></tr>
<tr><td align="center" colspan="2" class="shrink"><font size="1"><a href="/cgi-bin/mailpage2.cgi?action=privacy" target="_new">Click here</a> to read Clarity's privacy policy</font><br><br></td></tr>
<tr><td colspan=2 align=left class="shrink">Please enter...</td></tr>
<noscript>
<tr><td class="shrink">The URL of the page you would like to recommend:</td><td><input type="text" name="url" class="bigyellow"></td></tr>
</noscript>
<tr><td width=40% class="shrink">Your name:</td><td><input type="text" name="sendername" class="bigyellow"></td></tr>
<tr><td class="shrink">Your email address:</td><td><input type="text" name="senderemail" class="bigyellow"></td></tr>
<tr><td class="shrink">Your friend's name:</td><td><input type="text" name="recipient" class="bigyellow"></td></tr>
<tr><td class="shrink">And your friend's email address:</td><td><input type="text" name="recipientemail" class="bigyellow"></td></tr>
<tr><td align="left" colspan="2" class="shrink">Would you like to include a personal message?<br>If so, please type it here:</td></tr>
<tr><td align="center" colspan="2"><textarea cols="30" rows="5" name="message" class="bigyellow"></textarea></td></tr>
<tr><td align="left" colspan="2" class="shrink">How would you like to send the page?<br>
(If your friend can't read html email, choose the 'attachment' or 'link' option.)</td></tr>
<tr><td align="center" colspan="2"><select name="how" class="bigyellow"><option value="url">As an link to the website<option value="body">Included in the email<option value="attachement">As an attachment</select></td></tr>
<tr><td align="center" colspan="2"><input type="submit" value="Send email!" class="bigyellow"></td></tr>
</table>
</center>
</form>
With this version, the html looks fine, but the form doesn't work because the next line of hidden input - name="action" - is omitted. If I move that line up before the script, the html is garbled, as the opening table tag disappears.
I've spent much of today (OK, all of today :( ) experimenting with an alternative method, btw, and I'd certainly welcome any comments on it.
It begins:
<form action="$ENV{'SCRIPT_NAME'}" method="post" name="mailpage">
<INPUT TYPE="hidden" NAME="url">
<SCRIPT LANGUAGE="javascript">
<!--
document.mailpage.url.value=opener.location.href;
//-->
</script>
...then continues as before. I found that the script had to go in exactly this position, ie after the 'url' input, if it was to fill it in. And this one does work - about half the time. Sometimes the url of the opener comes through, sometimes it doesn't - without any changes in the script.
This is the version that's online now as I head off to bed, with the input changed to 'text' rather than 'hidden' so you don't have to send emails to find out whether it's working. If you visit
http://www.onlineClarity.co.uk/practical_I_Ching/what_is2.html
and click the 'send to a friend' button, you either will or won't see the opener url in the first text box in the popup...
I've just found a site that sells a script that does what I want for about $30 (passing the url to the popup is part of the cgi script). Do you think this might be a good idea? :rolleyes:
Thank you for taking an interest, and sorry about the huge post.
Hilary
glenngv
01-06-2003, 06:05 AM
try putting it onload:
<body onload="document.mailpage.url.value=opener.location.href"
<form action="$ENV{'SCRIPT_NAME'}" method="post" name="mailpage">
<INPUT TYPE="hidden" NAME="url">
btw, I was just curious what browser you are using.
Hilary
01-06-2003, 12:13 PM
Hi Glenn,
My browser is IE5.5 (on Win98): your suggested alteration works for me; the version I gave before (with the script immediately after the input) also worked pretty consistently. (It only failed a couple of times - I re-uploaded, and now it seems to work all the time.) My husband's browser is IE5.01 (also Win98), and neither version works on his machine. I read somewhere that 'opener' wasn't recognised by Netscape 3, but this is ridiculous.
I don't know whether the failures are browser-related or just random (I never used to believe in 'random' script failures, but...). What do you think?
Thanks for your help.
Hilary
glenngv
01-07-2003, 02:39 AM
I think IE5 is really buggy. But try
<body onload="document.mailpage.url.value=window.opener.location.href">
if that would make a difference in that browser.
Hilary
01-07-2003, 09:50 AM
Sorry, Glenn, I should have been more clear. I tried it 'onload' with no success in IE5.
glenngv
01-08-2003, 02:18 AM
Is there any error message?
Hilary
01-08-2003, 10:52 AM
No visible error messages - but then the popup window doesn't have a status bar, by default. And can I remember how to change this?
I'm feeling guilty that you're still spending your valuable time thinking about this. If I really want a reliable, cross-browser solution, I can spend those $30 and get a cgi script that does the work server-side. I shouldn't be keeping you fiddling with buggy javascript.
glenngv
01-09-2003, 02:05 AM
According to this reference, opener was first supported in IE4 and Netscape 3. So I think the error is somewhere in your page. Maybe the display of error msgs is turned off in your browser that's why you don't see any error.
Ooops! forgot to add the link :D
http://www.irt.org/xref/Window.htm
whammy
01-09-2003, 02:13 AM
Glenn is a genius... believe me, if he is offering to help, don't turn it down. :)
(BTW, that's $5 more dollars!) ;)
I have also had problems with "opener" syntax regarding forms, especially with XHTML and different browsers when only using "id" with the form tag instead of the deprecated "name" attribute... luckily though, this was on an intranet application where I know everyone is using IE, so it wasn't a big deal.
If I figure out what's causing that, I'll let you know. If someone here doesn't beat me to it. :)
Hilary
01-09-2003, 10:22 AM
Well, in that case, I'll say 'thank you very much' and persevere for a while. :)
So, I have a really, really basic question. The window is opened with
function openpopup(){
var popurl="screwy.html"
winpops=window.open(popurl,"","width=500,height=500,")
}
How do I change this so the status bar appears? (Unless there is any other way of seeing an error message?)
I should maybe also mention that the html is written partly direct from the script (the form), partly copied by it from a tokenfile (the outline of the page). In practice so far this doesn't seem to have made any odds - everything works/doesn't work the same whether the script looking for the opener is in the tokenfile or script-written section. Just thought it might be relevant some time...
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.