0

I have a file - filedata.php. The file shows only a CLICK ME link when opened in the browser. On clicking the CLICK ME link a popup window should appear which has the source code of filedata.php. I wrote the following code but it isn't working (no pop-up window appears). Please help me to figure out the error.

SCRIPT -

<!DOCTYPE html>
<html>
<script src="jquery-1.9.1.js"></script>  
<script>
$(document).ready(function() {
   $("a.xy").click(function (event) {
        //Prevent default behavior
        event.preventDefault();
        var js_array = <?php echo json_encode($php_array) ?>;
        var disp = window.open('','','width=400,height=400');
        $(disp.document.body).text( js_array.join("\n") );
    });
});
</script>

PHP CODE -

<?php
// Get a file into an array.
echo '<a href="" class="xy" onclick="openWin()">CLICK ME!</a><br>';
$path='C:\wamp\www\directory_listing\filedata.php';
$lines = file($path);

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
    $php_array[$line_num]="Line <b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />";
}

$html = implode('', file($path));
$trimmed = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>
</html>
Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
Rajarshi Sarkar
  • 199
  • 2
  • 13

2 Answers2

0

it is possible to communicate with new window but it is not possible to pass dom to new window.You would basically need to convert the generated DOM to a string, pass it across to the new window and then parse it as though it was a document.

Like This

Community
  • 1
  • 1
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
-1

Your hava used the wrong way to append html code, change $(disp.document.body).text( js_array.join("\n") ); to disp.document.body.innerHTML = js_array.join("\n");.

Below is all my code:

<!DOCTYPE html>
<html>
<?php
// Get a file into an array.
echo '<a href="" class="xy" onclick="openWin()">CLICK ME!</a><br>';
$path='C:\wamp\www\directory_listing\filedata.php';
$lines = file($path);

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line)
{
    $php_array[$line_num]="Line <b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />";
}

$html = implode('', file($path));
$trimmed = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
   $("a.xy").click(function (event) {
        //Prevent default behavior
        event.preventDefault();
        var js_array = <?php echo json_encode($php_array) ?>;
        var disp = window.open('','','width=400,height=400');
        disp.document.body.innerHTML = js_array.join("\n");
    });
});
</script>
</html>
wander
  • 937
  • 1
  • 7
  • 15