简体   繁体   中英

document.title and docuemnt.write both are not working simultaneously

I want to open a new window on click of a button. It is opened successfully but I can't write data and set title simultaneously.

Does anybody have any idea?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title And Data Both</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
   $(document).ready(function(){
       $("button").click(function(){
            var filename ="console.log";
           var newwin = window.open("","","width=200,height=100");


            newwin.document.title="Title";            
            newwin.document.write("<h1>XXXX</h1>");
       });
   });
</script>
</head>
<body>
<button type="button">Get Substring</button>
</body>
</html>

The document.write() overwrites all existing HTML( including the title ). One way you could do it, is to set the title after the write.

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title And Data Both</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("button").click(function () { var filename = "console.log"; var newwin = window.open("", "", "width=200,height=100"); newwin.document.write("<h1>XXXX</h1>"); newwin.document.title = "Title"; }); }); </script> </head> <body> <button type="button">Get Substring</button> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM