最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - trying to use console of a new window - Stack Overflow

programmeradmin4浏览0评论

I'm beginner to learn JS and I am stuck with the following function.

I'm trying to open a new window and print in the console of the new window. My code is as follows:Can you please tell me where's the error ?

var newWindow = window.open("url");
newWindow.onload = function (newWindow) {
  newWindow.console.log("...something...");
}

I'm beginner to learn JS and I am stuck with the following function.

I'm trying to open a new window and print in the console of the new window. My code is as follows:Can you please tell me where's the error ?

var newWindow = window.open("url");
newWindow.onload = function (newWindow) {
  newWindow.console.log("...something...");
}
Share Improve this question edited Jan 3, 2016 at 9:48 Evaldas Buinauskas 14.1k11 gold badges61 silver badges118 bronze badges asked Jan 3, 2016 at 9:35 karan miranikaran mirani 1881 gold badge3 silver badges8 bronze badges 7
  • Why do you need to access the console of the new window? – Adam Azad Commented Jan 3, 2016 at 9:38
  • 1 newWindow.onload = function (){ console.log("...something..."); } try it – PaulShovan Commented Jan 3, 2016 at 9:39
  • I believe this post is a possible duplicate. – ipinak Commented Jan 3, 2016 at 9:41
  • @gypsyCoder doesn't seem to work :( – karan mirani Commented Jan 3, 2016 at 9:51
  • is it throwing any error? – PaulShovan Commented Jan 3, 2016 at 9:52
 |  Show 2 more ments

4 Answers 4

Reset to default 1

After the code

var newWindow = window.open("url");

It should instantly open another window with "url" path. What happens after is field of work of "url" page. In this page you should have on document ready function which is similar to

newWindow.onload = function (newWindow) {
    newWindow.console.log("...something...");
}

Which could be simple jQuery function

$( document ).ready(function() {
    console.log( "ready!" );
});
<html>
<script>
function myFunction() {
var myWindow = window.open("", "MsgWindow", "width=200, height=100");
myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");
}
myFunction();
</script>
</html>

Write this code in a HTML file and run it, make sure pop-ups are not blocked.

The result you want to get from the code is not possible. Let's call the window where you are executing javascript window 1 and you are opening a fresh new window window 2. Now, window 2 neither has reference of your js code nor has the onload event. onload event of window 2 is firing at window 1 instance. So, you are getting the console log at window 1 instance not in window 2.

See the fiddle's console for window 1 instance.

If you were to create a new window with the same domain (or no url), you can use the console just fine:

var newWindow = window.open("");
newWindow.console.log("test");

Example: https://jsfiddle/8p2fk4j0/ (disable popup blocking)

发布评论

评论列表(0)

  1. 暂无评论