0

I have some JavaScript that is being used to open a new window to print part of page I am using the window.open function and I have set the title of the page as window.open 2nd parameter (see code below),but the title appear 'about:blank' Not 'test'

<script type="text/javascript">
    function printPartOfPage(elementId) {
        var printContent = document.getElementById(elementId);
        var windowUrl = 'about:blank';
        var printWindow = window.open(windowUrl, 'test', 'left=50000,top=50000,width=0,height=0');


        printWindow.document.write(printContent.innerHTML);
        printWindow.document.close();
        printWindow.focus();
        printWindow.print();
        printWindow.close();
    }
</script>

Note: i know the question might be repeated but i try all answers it doesn't work for me Like:

1-Set the 1st parameter of open() to 'about:blank'

2- printWindow.document.title = 'test'

the result as shown below

enter image description here

Eman Shedeed
  • 114
  • 2
  • 9

2 Answers2

3

the problem is you put

printWindow.document.title = 'title'

before the printWindow.document.write
Solution is put printWindow.document.title after printWindow.document.write

<script type="text/javascript">
    function printPartOfPage(elementId) {
        var printContent = document.getElementById(elementId);
        var windowUrl = 'about:blank';
        var printWindow = window.open(windowUrl, 'test', 'left=50000,top=50000,width=0,height=0');


        printWindow.document.write(printContent.innerHTML);
        printWindow.document.title = 'This is the title'
        printWindow.document.close();
        printWindow.focus();
        printWindow.print();
        printWindow.close();
    }
</script>

Fiddle Here

Beginner
  • 4,118
  • 3
  • 17
  • 26
0

You can substitute a data URI of an .html document with <title> tag for about:blank, document.write(). Call print() within <script> tag set as last child element of <body> element.

function printPartOfPage(elementId) {
  var printContent = document.getElementById(elementId);
  var windowUrl = "data:text/html,<!DOCTYPE html><html><head><title>test</title></head><body>" + printContent.innerHTML + "<script>print()<\/script></body></html>";
  var printWindow = window.open(windowUrl, "test", 'left=50000,top=50000,width=0,height=0');
}

plnkr http://plnkr.co/edit/ZfqHgNnjAkunJeryn71Y?p=preview

using Blob(), URL.createObjectURL() to set window.open() URL to Blob URL

function printPartOfPage(elementId) {
  var printContent = document.getElementById(elementId);
  var html = "<!DOCTYPE html><html><head><title>test</title></head><body>" + printContent.innerHTML + "<script>print()<\/script></body></html>";
  var blob = new Blob([html], {type:"text/html"});
  var windowUrl = URL.createObjectURL(blob);
  var printWindow = window.open(windowUrl, "test", 'left=50000,top=50000,width=0,height=0');
  window.onfocus = function() {
    URL.revokeObjectURL(windowUrl);
    printWindow.close();
  }
}
guest271314
  • 1
  • 15
  • 104
  • 177
  • What are `Pop-ups` settings at `Settings`? Did you click `

    ` text content `"test"`?

    – guest271314 Nov 10 '16 at 06:23
  • No , it work for my answer but when change printPartOfPage to your it doesn't appear – Eman Shedeed Nov 10 '16 at 06:26
  • Allow All site to show popup i don't understand second question – Eman Shedeed Nov 10 '16 at 06:38
  • To allow plnkr origin to open popups. Have you checked `Pop-ups` at `Setiings`? What are current browser pop-up preferences set to? – guest271314 Nov 10 '16 at 06:42
  • sorry, but pop-up section in settings have only 2 radio button to Allow Or not sites to show pop-ups where i can find 'browser pop-up preferences' – Eman Shedeed Nov 10 '16 at 06:49
  • @BentEl-Eslam See [chrome Pop-up blocker when to re-check after allowing page](http://stackoverflow.com/questions/40282861/chrome-pop-up-blocker-when-to-re-check-after-allowing-page/) – guest271314 Nov 10 '16 at 06:53
  • i think that this is not related to my question as it's already open the window by using my code and i ask only about change page title so please illustrate your point of view Note:i disabled AdBlock on this page – Eman Shedeed Nov 10 '16 at 07:02
  • Both `javascript` approaches open new `window` containing `document` having `title` set to `"test"` where `print()` is called, here, at chromium 52. Are pop-ups allowed at the browser you at trying at for domain plnkr? – guest271314 Nov 10 '16 at 07:04
  • @BentEl-Eslam What are pop-up settings for domain plnkr? – guest271314 Nov 10 '16 at 08:21
  • what is domain plnkr ? sorry i really doesn't get the point , i open your link [link] (http://plnkr.co/edit/ZfqHgNnjAkunJeryn71Y?p=preview) , see that Plnkr is an editor you try on it and it work , for me in my app it doesn't work and i don't know how can i get pop-up settings for domain plnkr ? – Eman Shedeed Nov 10 '16 at 08:38
  • What do you by _"it doesn't work"_? Can you reproduce what does not work for you at plnkr? – guest271314 Nov 10 '16 at 08:45
  • plnkr is domain of editor. run.plnkr is another domain that edited pages run on. – guest271314 Nov 10 '16 at 08:52