I have a button at the bottom of a form that when is pressed I would like to open up a new URL in the same window. Right now with the code I have it opens up a new window /tab. If I put ,"_self" after the url, the button breaks completely. Here is my code
$(".study-btn").click(function(){
if(questionCounter == 3)
{
window.open("http://www.google.com");
// I tried this but breaks completely
window.open("http://www.google.com", "_self");
}
else
{
window.open("http://http://www.amazon.com");
}
});
The question counter is just to me to figure out which of the form questions have been answered YES or NO.
Using this should work to open a website in a new tab in the same window:
window.open('www.google.com','_target');
If you just want to change the current page, use:
location = 'http://www.google.com';
This should work if you want to stay in the same window. window.open AFAIK usually opens a new "pop-up" window, which you don't need if you want to stay in the current window.
$(".study-btn").click(function(){
if(questionCounter == 3)
{
location.href="http://www.google.com";
}
else
{
location.href="http://http://www.amazon.com";
}
});
Related
I'm making a product category page similar to this one https://www.stylight.se/Skor/Herr/. I want to be able to send visitors to a different page upon clicking on a product, and the page should open in a new tab (just like on the page I've linked).
However, when I try to replicate that with Ajax I get a one second delay between clicking on the product and the tab actually opening, due to the time it takes to query my database for the URL for the product clicked.
I notice that on the page I linked to, the tab opens instantly with "about:blank", and only seems to redirect to the URL in question after that.
I'm currently doing window.open(response) with the response from the ajax call.
Is it possible to open a new tab first and then redirect that tab to a new URL, and if so, how can it be done with javascript? Alternatively - how does the aforementioned page accomplish this?
You can use the value returned by window.open, as it is a reference to the opened window object.
var newWindow = window.open();
newWindow.location.href = 'https://google.com';
They use this code
static goToUrlBlank(url) {
if (url) {
let newWindow = window.open(url, '_blank');
if (newWindow !== undefined && newWindow !== null) {
newWindow.focus();
} else {
// fallback for browser blocking the opening in a new tab
window.location.href = url;
}
}
}
I want to show popup only when user close the browser window. But, In the following code. It showing popup when I refresh the page.
My Code
<script type="text/javascript">
$(document).ready(function()
{
var key = true;
$(window).bind('beforeunload', function(e){
if(key)
return confirm("Do you really want to close? This will log you out of your profile.");
e.preventDefault()
});
$(document).on("click","a",function() {
key=false;
});
});
</script>
This is essentially what you need.
Source: open a custom popup on browser window/tab close
JSFiddle: http://jsfiddle.net/SQAmG/5/
var popit = true;
window.onbeforeunload = function() {
if(popit == true) {
popit = false;
return "Are you sure you want to leave?";
}
}
EDIT
Use HTML5 Web Storage to simulate sessions within the browser. Here you would want to use sessionStorage rather than localStorage so the session ends when the browser window is completely closed.
If the sessionStorage variable is set, do not start the popup, otherwise allow the popup code to execute.
HTML5 Web Storage: http://www.w3schools.com/html/html5_webstorage.asp
If you set onbeforeunload callback, it will be executed every time whether you close the tab or refresh the page. So, lets get into some hack here. When we close the tab, the window loses its focus. So Inside onbeforeunload we will just check if the window has focus. If it has, you are refreshing the page. If it has lost focus, probably you have closed the tab.
window.onbeforeunload=function(){
if(!document.hasfocus()){
//show popup
}
}
I have read a bunch of posts but none of them seem to answer the question that i have exactly.
Is it possible to detect a tab closing that was opened by a target="_blank" attr?
I need a new tab/window to open, the user will select an option, then the tab closes.
When that tab closes i need the original window(parent or opener) to refresh.
Any ideas?
EDIT:
further explaination
i have an application that opens another application in an iframe. the application opening in the iframe connects to social media for posting a share. the new tab that opens is used for authentication/oauth dance. the page that is open in the main window shows the accounts that are connected and allows you to connect new accounts(in the main window in the iframe). the authentication/oauth opens a new window/tab to do the dance, then it closes itself. i need the parent window(main window with iframe in it) to know that the new tab/window closed so it can refresh the content and reflect the new connect.
FINAL NOTE
I use many authentication methods, so really the above is irrelavant.
I just need to find out a way to monitor the new tab/window that has opened or even have the new window/tab fire a browser event onunload to that the parent/main window knows it closing so it can refresh.
So if anyone is interested, this is how I have it working for now until I find a better solution.
First what I did was had javascript open my window by assigning it to a variable so it had a reference name.
var checkWindow = false;
var fbAuthWindow;
function openFBAuth(url) {
fbAuthWindow=window.open(url);
checkWindow = true;
setTimeout(function() { checkAuthWindow('fb'); }, 1000);
}
That also sets a timeout to run every second while checkWindow == true to check if the window is closed.
If the window is closed (my script closes the window when authentication is complete), then the page reloads ultimately setting checkWindow back to false.
function checkAuthWindow(win){
if(checkWindow == true){
if(win == 'fb'){
if(fbAuthWindow.closed){
window.location.reload();
} else {
setTimeout(function() { checkAuthWindow('fb'); }, 1000);
}
}
}
}
It isn't the prettiest or by any means a best practice to have the checking the window every second, but for now it will work until i find a better solution.
Here is the working example (http://jsfiddle.net/sureshpattu/w8x7dqxr/) and code
Html
<button class="openPopActBtn">Open pop window </button>
Javascript
var checkWindow = false;
var fbAuthWindow;
$('.openPopActBtn').click(function() {
openFBAuth('www.google.com');
});
function openFBAuth(url) {
fbAuthWindow = window.open(url);
checkWindow = true;
setTimeout(function() {
checkAuthWindow('fb');
}, 1000);
}
function checkAuthWindow(win) {
if (checkWindow == true) {
if (win == 'fb') {
if (fbAuthWindow.closed) {
window.location.reload();
} else {
setTimeout(function() {
checkAuthWindow('fb');
}, 1000);
}
}
}
}
I have a confirm dialog appearing on a form, which redirects the user to another page when clicked on ok.
However, is it possible to have that page open in a new window (e.g. target="_blank")
The code that I'm using is:
var answer=confirm("Do u want to continue?") {
if (answer==false) {
document.form.field1.focus();
return false;
}
window.location.href = "/some/url"
}
So window.location refers to the location of your current window, so changing it of course takes you away from the window you're on. To open a new window, you want window.open - see Open a URL in a new tab (and not a new window) using JavaScript
However, I'd suggest instead, using a link that you intercept the default behavior for if the user answers false, and otherwise fallback to the default behavior.
ie:
bar
$('#foo').click(function(e){
var answer=confirm("Do u want to continue?") {
if (answer==false) {
document.form.field1.focus();
e.preventDefault();
}
}
});
Although I'd have to double check if that actually works, I haven't worked with confirm much at all, so you might have to call the e.preventDefault() outside of that block.
window.open(
'/some/url',
'_blank'
);
This is the current code I am using:
<script type="text/javascript">
var win = window.open("URL...", "thePopUp", "");
var pollTimer = window.setInterval(function() {
if (win==null || win.closed !==false || navigator.appName=="Microsoft Internet Explorer"){
window.clearInterval(pollTimer);
pause();
alert("Oh No! Something has gone wrong, please check the requirements before continuing.");
}
}, 10);
</script>
That basically opens in another window a page that users need to leave open to earn points on the site. The popup window that is shown changes the URL every 20 seconds.
However it automatically opens when they open the page, I would like to use a button to start that above code off. But when I tried this the popup window doesn't change the URL unless they press the button again.
So what I'm after is a way to open this popup window with a button but to not have to then use the button again and again afterwards so it doesn't go through the button any more.