prevent child window from being caught by popup blocker - javascript

I have a function which upon click, it checks some information with a $.get() call before it can know if it should open a child window. It works fine, only that, because the function which opens the child window was not directly from the click event, it will be considered a pesky unwanted popup. Does anyone know of a work-around or a way to prove to the browser that it is a wanted window? Thanks.
$('#send').click(function(){
$.ajaxSetup({cache: false})
$.get('test3.html',function(data){
if(data == "processing"){
alert("still processing");
}else{
childPage = window.open("test.html","send","width = 300,height = 300");
}
});
});

It would be more reliable to pop up the window immediately (to show the user something's hapenning) with a loading screen. Then in the ajax handler you can redirect the popup to the correct page.
That said, popups in general are a bad idea. Is there no way you could do this another way?

I know that I'm not exactly answering what you want, but I found this way more appealing in general than a normal pop up, and gaining way more control.
I'm talking about using a plugin like fancybox, http://www.fancyapps.com/fancybox/#examples
witch allows you to embed iframes, or any other content you want. I'm pretty sure loading an iframe like this would be much easier than trying to sneek around browser limitations.

Does anyone know of a work-around or a way to prove to the browser that it is a wanted window
This will mean changing the browser settings, these are there to prevent what you are trying to achieve so unfortunately to my knowledge there is no way.

Instead of open a new popup, you also can try simulate a popup window via tag. Try this

Related

I want to open a new window on my current window using JS

I am using the code shown in the images below. I have tried many ways like closing login window, before opening a new one. I have also tried many ways like window.open("index.HTML", '_self', false) and window.location.replace("index.HTML") but they are not working at all:
I usually use window.open("http://www.example.com", "_blank")
Also check your pop-up blocker since it will most probably detect this as popup and block it.
If not then use a monkey patch which is create an html anchor tag with target="_blank". Hide it with CSS visibility: hidden, then invoke the click event using JS.
Read more about popups and blocking in this answer. If your window.open event was not triggered by a click then most probably it would be blocked.
What you want to do, if I am not mistaken, is open a new webpage but using the same window (or tab)... To do this, you should modify the window.location property.
For example:
window.location = "new_page.html"
This will take you to new_page.html.
If your current location is: /users/muhammad/index.html
Then the page will take you to: /users/muhammad/new_page.html
Leave a comment if you have any questions!
=)

Can I refresh my parent window when closing the popup?

I want to refresh my parent window when I close the popup.I could use following
window.opener.location.reload();
But my requirement is slightly different in that I have to write all this code for refreshing the parent page in the parent page itself.
I cannot write a single line of code in the popup.
Thanks,
The only way I know is by checking child window status at frequent intervals but this is explained clearly in this question
That said, I don't know if that could be an alternative, but using a lightbox instead of a new window popup would allow you to keep full control on your events as the whole thing stays in the same window.
Most lightbox API's offer that kind of functionality (loading an external site in the lightbox instead of the usual image), using dynamically generated iFrame to display the external site. This solution also have drawbacks (e.g.: frame-busting code on site loaded in lightbox) but can look nicer than a plain old new window...
I've been using Shadowbox on projects for quite some time now and always liked it, but there are plenty of others out there, maybe even better.
You need to handle the unload event handler in the pop-up and do the reloading in the main window. In the main window, add JavaScript:
function popUpClosed() {
window.location.reload();
}
In the pop-up:
window.onunload = function() {
if (window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
};
So the answer to your question is generally no, if you need your code to work in all browsers, in particular IE.

Is there a way to make sure target = _blank links open in foreground?

For some browsers (and depending on settings), links with target = "_blank" seems to open in the foreground, but for a lot of others it seems to open in the background.
In my case opening in the foreground is what a user intends when she/he clicks on the link, so it would be nice if there is a way to ensure that. Is there a way to do this? Thank you.
There is no way to make sure these links open in the foreground. This is something the browsers arrange and there is nothing you can change about it.
You could however open the link in the same window by leaving out the target attribute, that will ensure the people clicking the link get to see the page immediatly. In a way this is also 'nicer' since it leaves the choice of opening the link in a new window to the user itself.
You can try using window.focus() to bring the new window to the front. You may do it on the opened document, using jquery, with:
$(function() {
window.focus();
});
or from the original document:
var newWindow = window.open(url);
window.setTimeout(function() { newWindow.focus(); }, 1000);
The setTimeout is needed to allow time for the window to be actually created by the browser. There may be a cleaner method to achieve this, but the setTimeout should do it.

how to make popup windows always on top?

I want to have a javascript/jQuery popup window (child page) that is always in front of the parent page, something like facebook's current picture viewing feature.
it would be closed by the user clicking on the close button.
I have tried as below:
mywindow = window.open ("DownloadForm.aspx", "mywindow","location=1,status=1,scrollbars=1, width=350,height=150");
mywindow.moveTo(350, 350);
this code successfully opened a child page in front of the parent page, but there is jQuery code ( $(#test).click() ) on the parent page which causes the parent page to always be in front.
I tried putting the window.open() code after $(#test).click(), but it didn't solve the problem.
The $(#test).click() is necessary, therefore I need to have a workaround.
I appreciate any help, thank you in advance.
myWindow.focus()
Many modern browsers prevent this from working, but it's the only way.
use following javascript code in the page you are opening in popup
<body onblur="self.focus();">
Im sure you cannot control the order of the window. Instead of a popup (which is often and usually blocked by browsers) why not investigate using a lightbox javascript plugin. They are usually free, stylish and work well.
Just set it from the modal css. The z-index is normally set to 1. Just change it to 9999. That is the purpose of the z-index, to set the popup top level.
If not using bootstrap popup, then you need to use css to set it. Browsers are not guaranteed to place nice with .focus() on a window.

Disable "history.go(-1)" on certain pages

I want to be able to disable history.go(-1) on certain pages.
I'm wondering if there is a way to clean up the Javascript's history object. Any ideas?
history.go() seems to not be functioning too well on certain browsers such as IE. Is that true? Any solution for that?
You can over-write the go method, but it doesn't get you much
history.go = function(){};
This will disable all uses of history.go, and not just when you pass a -1.
And as others have pointed out - this doesn't stop the user from using the Back button on their browser.
The best you can do to "disable" history is to open your page in a new window, which will have no history. Pages opened in a new window don't have a back button enabled and history.go(-1) will not do anything
Short of that, there is nothing you would (or should) be able to do.
Revised to make an implication explicit. I thought I was being clear, but apparently I was not.
You cannot override this browser behavior. If you could there would be mayhem as malware would try to take over everyone's history.
If you're trying to disable the browsers BACK button this is simply not possible. Would you like it if websites could mess around with your browsers functionality?
You've got two options.
Load pages with post request, this will prevent users from using back-button as you can trap the reload on server-side. (You need to wrap all hyperlinks with js though, so a bit of work, and the solution is quite ugly.)
Use DOM to load content into page. History won't be able to backtrack it. (or am i wrong here :-| )
describe your problem more clearly, there's probably better solutions for where you need to go.
Put this in your shared page header:
<body onLoad="history.forward()">
It will always force the user to the most recent page they've visited on your website.
This is ugly, but might work:
$(document).load(function(){
document.location.href = document.location.href + "#";
setInterval(function(){
if (window.location.hash != '#')
document.location.href = document.location.href + "#";
}, 100);
});
ugh!
Desmond,
My boss doesn't want the back button to work on the landing page.I know it's stupid but what can I do.
What about using default page, (index.php or whatever) to point forward.
index.php:
header('location:start.html');
//or likewise with js,
document.location = 'start.html';
start.html:
my real start
as user hits the back-button, the index.php will redo the forwarding.
regards,
Thanks for all the responses. After two days of juggling, I came up this solution, for now.
I use PHP to output a class to the body tag if it's the current page is the front page. Something like
<body class='front'>
and then use jQuery to enable/disable "history.go(X)" on the fly.
$(document).ready(){
if($('body.front').length>0){
$('#backbutton').click(function(){ window.history.back(); });
}else{
$('#backbutton').unbind('click');
}
}
In fact the project that I was working on require the site inside an iframe to disable the element that triggers the back button of the parent window. The js becomes:
var backbutton=parent.window.getElementById('#backbutton');
$(document).ready(){
if($('body.front').length>0){
$(backbutton).click(function(){ window.history.back(); });
}else{
$(backbutton).unbind('click');
}
}

Categories

Resources