window.open vs window.location.href - javascript

How do I decide whether to open my local web page window with window.open or window.location.href?

Simply,
window.open() will open a new window for your passing URL in side the parentheses.
window.location.href will redirect you to the passing URL with in the same window.

if window.opener == null
// open by window.location.href
else
// open by window.open

Related

How to get the URL of window.opener using Java Script

I have open a new window from another window. I need to get the parent (opener) windows url. Is there a way to get the window.opener url?
window.opener.location.href is what you want. However, you can only read this if it's in the same domain as the window you are calling it from.
It will be redirect to another page.
window.location = "https://www.w3schools.com"
Visit W3Schools!
document.referrer seems to do the trick
Some tests:
if (window.opener) {
console.log(
window,
window.opener,
window.opener === window,
document.referrer, // Works!
// window.opener.location.href, // crashes
// window.opener.location.toString(), // crashes
// window.opener.document, // crashes
)
}

How to stop window.open from refreshing the window in javascript?

Lets say you have code like this.
function openNewTab(url) {
const newTab = window.open(url, 'windowNameHehe');
};
Now every time you call this function it will open a new tab. However, if the window is already opened it will go to it and refresh/reload it. How do I stop it from reloading/refreshing and just bringing it to view?
Doing something like newTab.focus() or adding the boolean (true/false) in the 4th parameter of window.open isn't working for me either. Any help?
function openNewTab(url) {
var newTab = window.open(url, 'windowNameHehe', , false);
newTab.focus();
}
https://www.w3schools.com/jsref/met_win_open.asp
Open a URL in a new tab (and not a new window) using JavaScript
function openNewTab(url) {
const newTab = window.open('', 'windowNameHehe');
newTab.focus();
if (!newTab.location.hostname) {
newTab.location.href = url;
}
};

Click button to open url in current window

I am using Angular and tried to create a function to open url in current window. Below code in the controller actually opens new window instead:
$scope.openUrl = function(url) {
$window.open(url);
};
..when I use this ng-click='openUrl("someurl")'
So how to open in current window? I tried ng-click='parent.open()' and it doesn't work.
You could achieve this by using same old vainilla javascript:
$scope.openUrl = function(url) {
window.location = url;
};
$window is a wrapper for the browser's native window object. You can use this low-level method to actually change the location:
$window.location.href = url;
$location is a bit different. It can only extract from and manipulate the browser URL string. Its purpose isn't to actually navigate.
What does it not do? It does not cause a full page reload when the
browser URL is changed. To reload the page after changing the URL, use
the lower-level API, $window.location.href.
https://docs.angularjs.org/guide/$location

Window.open doesn't return the window reference IE9

I'm using a script to mount my mailto link and call the default email editor. But I can't use document.location.href because of some bug in IE9, so I use window.open. It works. But I need to close the IE windows opened.
The problem is the window.close doesn't return the window reference.
function doMailto() {
var sMailto = 'mailto:?bcc=';
sMailto += document.getElementById('<%= txtEmails.ClientID %>').value;
out = window.open(sMailto);
out.close(); //CANT CALL CLOSE, BECAUSE OUT IS NULL
}
You have an extra paren at the end of the assignment line.
Also, are you trying to close the window in the same function as assigned? If not, you may need to declare the window variable outside the function so it can be closed when needed.
aside the syntax errors (two commas, two parentesis closing)...
you are opening a window that is external to the browser, your default mail client. You cannot control it through javascript.
Maybe it's an immediacy problem, try using:
var out = window.open()...
setTimeout(function(){out.close()}, 200)
and fiddle with the 200ms to see if it works then.
try
top.location.href = 'mailto:....';
you won't need to open or close any windows this way

How to check whetherpop up blocker is turned ON or not in a browser using Code ?

How to check whether pop up blocker is turned ON or not in a browser using java or Java script Code ?
function check ()
{
document.login.action= url+"test.jsp";
document.login.submit();
}
I will call this function on click of submit button
How about:
var myWindow = window.open (url);
if (if (myWindow == null || typeof(myWindow )=='undefined'))
{
// popup blocker is enabled
}
else
{
myWindow.close();
}
If you use window.open() to open the popup, check for the return value.
According to the MDC doc center (a good javascript reference) the return value is null if opening the window didn't succeed for whatever reason.
var windowReference = window.open(url);
See the documentation on window.open here.

Categories

Resources