Question

I read a book about javascript and it says that when we create a function for opening a new window, the reference to the new window object is stored inside the variable assigned to the window.open(theURL , newWindow) call. It also says that if the same variable is used for two or more URLs, no matter which URL I click it would be open in the same window. However, if I create two variables for two window.open() calls, both sites will open in different (new) windows.

Due to curiosity, I tried to use two different variables, but still when I click both links they are still loaded in the same window. How come? I've tried it on Firefox, IE and Chrome to no avail.

Was it helpful?

Solution

The book is partly correct, however what you actually need is two different values, not really two different variables. If you have two variables with the same value, the result is the same as using the same variable. It's the value that matters, not what variable you use to supply the value.

The second parameter to the open method is the target. If you use a value like for example 'win42', that will be used as the name for the window. If you call open again with the same value for target, it will open in the same window.

You can use the special value '_blank' as target to always open a new window.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top