Forum Moderators: open
[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
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.
Tests for the opener.document or opener.closed should work.
is this the right way to go about it?
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.