Forum Moderators: open

Message Too Old, No Replies

is this the best way

code works...for now

         

WhosAWhata

9:48 pm on May 12, 2004 (gmt 0)

10+ Year Member



i have the following PHP file, though i'm more concerned with the JavaScript (so i posted here)

[quote]<?
$dir = "../pics/";
$d = dir($dir);
while($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
$p[] = $f;
} }
$pn = count($p);
if($pn) {
foreach($p as $n => $v) {
$m = $n % 4;
if(!$m) $h .= "<tr>";
$h .= "<td><a href=\"#\" onclick=\"javascrpt:pic('".$v."')\"><img src=\"".$dir.$v."\" width=150></a></td>";
if($m == 3) $h .= "</tr>";
}
if ($m!= 3) $h .= "</tr>";
} else {
$h = "<tr><td colspan=4><h1>No Pics Have Been Uploaded</h1></td></tr>";
}
?>
<html>
<head><title>Add Pictures</title>
[quote][b]<script>
function pic(pic) {
window.opener.document.form.article.value = window.opener.document.form.article.value+'\n[img='+pic+']\n';
self.close();
}
</script>[/b][/quote]<link rel="stylesheet" type="text/css" href="style.css"></head>
<body><center><h1>Select Picture</h1><br><table width="85%" cols="4">
<?=$h?></table><a href="#" onclick="javascript:self.close()">[x] Close</a>
</center></body></html>[/quote]

for those of you that don't know PHP, basically all it does is find the files in my picture directory and output each of them like this
<a href="#" onclick="javascript:pic('PICTURE-NAME')"><amg src="PICTURE-LOCATION" width=150></a>

the JavaScript is in bold and an extra [ quotes ] thing

i'm just wondering if this is effective and if there are any security flaws i should be aware of (i am already aware that it works if properly called)

thanks for any advise, input, comments, and suggestions

WhosAWhata

10:00 pm on May 14, 2004 (gmt 0)

10+ Year Member



i'm also wondering if it would be wise to add a statement to makesure the window.opener is existent and still open, otherwise redirect to the window.opener's path...is this necessary/propper

Bernard Marx

2:26 pm on May 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would hope that your users can be trusted not to close the original window, but I'm sure it'll happen. If you want to code to take care of that you might have a struggle on your hands, unless you want them to simply start again. In the end, it's up to you.

Tests for the opener.document or opener.closed should work.

window.opener.document.form.article.value = window.opener.document.form.article.value+'\n[img='+pic+']\n'; 
// the shorter form should work
window.opener.document.form.article.value += '\n[img='+pic+']\n';

You don't need the 'javascript:' protocol in an 'onclick'.

I'm a little worried about using the name (pic) as an argument in a function of the same name. It doubtless still works, but take care with that.

WhosAWhata

5:58 pm on May 15, 2004 (gmt 0)

10+ Year Member



just out of curiosity, what would make you careful about function pic(pic)?
i'm new to javascript and i typically only write my own server side scripts.
Tests for the opener.document or opener.closed should work.

how might i go about testing this?
what i'm thinking is if they don't have the original open, the page should automatically redeirect itself to it
function open() {
if (document.isn't.open) {
window.location='opener.php';
return false;
} else {
return true;
}
}
<body onload="open()">

is this the right way to go about it?

Bernard Marx

9:41 pm on May 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



just out of curiosity, what would make you careful about function pic(pic)?

In JavaScript, functions are fully fledged objects. There already is a global variable

pic
that holds a ref to the function.( try
alert(pic)
). If you used
pic
as a global variable with a new value, then this will replace the reference to the function, and you wouldn't be able to call it. Using it as an argument name - thus a local variable - should be OK.

For much the same reason, you shouldn't call your new function

open
, as you have. Functions are window methods, and there is already a window method called
open
that is used to open windows! That doesn't mean your new function won't work. It just means that you will have lost the ability to use the normal
open
method in that window.

..apart from that, the condition in the function could be

(opener.closed) or
(!opener.document) perhaps even
(!opener)
..I haven't tried.

It seems you are testing for the opener having been closed as soon as the new window has loaded. Is that what you want?

Oh, and you may need to set the location to an absolute URL.

WhosAWhata

12:10 am on May 16, 2004 (gmt 0)

10+ Year Member



perfect, just what i was looking for
and thanks for that explaination
it was just what i wanted to know