0

i have a.html which is

a.html

    <script>
    function printContent(el){
        var restorepage = document.body.innerHTML;
        var printcontent = document.getElementById(el).innerHTML;
        document.body.innerHTML = printcontent;
        window.print();
        document.body.innerHTML = restorepage;
    }
    </script>
<div id="div1">DIV 1 content...</div>
<button onclick="printContent('div1')">Print Content</button>

i want to print contain in a.html which the button is in b.html

b.html

<a href="#" onclick="printContent("printJS")">Print</a>

How to pass the func to other file?

thanks

  • See here: http://stackoverflow.com/questions/4818854/javascript-import-html-is-it-possible. What you are doing doesn't make much sense. You can have your functions in separate files and import those js scripts. – VSO May 20 '17 at 20:22
  • Are you trying to print the contents of the other window? – Jared Farrish May 20 '17 at 20:31
  • yes, of course @JaredFarrish how to do it? – Herlambang Permadi May 20 '17 at 20:32
  • This question gives you the general idea, you can also print up with things like `top.print()` or `window.parent`: http://stackoverflow.com/questions/9852190/js-window-open-then-print – Jared Farrish May 20 '17 at 22:29
  • Plus, if you're trying to push contents from your current window into another, skip that and use stylesheets or a "print div". It's easy enough to google for that. – Jared Farrish May 20 '17 at 22:30

1 Answers1

2

Extract your function to separate *.js file and include this file in both html files.

<script src="myscripts.js"></script>
MDD
  • 134
  • 8