Create a new window object javascript
Here is an example of how you can create a new window object in JavaScript:
// Create a new window object
var myWindow = window.open("", "myWindow", "width=400,height=300");
// Set the title of the window
myWindow.document.title = "My New Window";
// Set the content of the window
myWindow.document.write("<h1>Welcome to my new window!</h1>");
This code creates a new window object using the window.open()
method, which takes three arguments:
- The first argument is the URL of the page to load in the new window. In this case, we're passing an empty string (
""
), which means the new window will be blank. - The second argument is the name of the window. In this case, we're passing the string
"myWindow"
. - The third argument is a string of attributes that specify the properties of the new window. In this case, we're setting the width to 400 pixels and the height to 300 pixels.
Once the new window is created, we can access its properties and methods using the myWindow
object. For example, we can set the title of the window using the document.title
property, and we can set the content of the window using the document.write()
method.
Note that the window.open()
method returns a reference to the new window object, which we assign to the myWindow
variable.
Also, you can use window.open()
method with "_blank"
as the second argument to open the new window in a new tab instead of a new window.
var myWindow = window.open("https://www.example.com", "_blank", "width=400,height=300");
You can also use window.open()
method with "_self"
as the second argument to open the new window in the same tab.
var myWindow = window.open("https://www.example.com", "_self", "width=400,height=300");
You can also use window.open()
method with "_parent"
as the second argument to open the new window in the parent window.
var myWindow = window.open("https://www.example.com", "_parent", "width=400,height=300");
You can also use window.open()
method with "_top"
as the second argument to open the new window in the topmost window.
var myWindow = window.open("https://www.example.com", "_top", "width=400,height=300");
It's worth noting that the window.open()
method is subject to the same-origin policy, which means that the new window can only be opened to a URL that is from the same origin (domain, protocol, and port) as the current page.