New window without focus - javascript

I know this question has been "answered" multiple times, but nothing works in my case. I have tried with focus(),blur(). ect...I am using IE 11. Anyone have any idea? I want to open a new window, but keep focus on the original window....
btn.addEventListener("click", function (ev) {
var url = "https://XXXX/XXXXX/XXXXXX/";
var newWin = window.open(url, "_blank");
window.focus();
ev.stopPropagation();
}, true);

Try to refer example below may work for you.
Code in parent page.
<html>
<head>
<script language="JavaScript" type="text/javascript">
function openPopUP() {
window.open('C:\\Users\\Administrator\\Desktop\\95.html','NewWin',
'toolbar=no,status=no,width=350,height=135')
}
</script>
</head>
<body>
Original Page:<br>
Click to open popup
</body>
</html>
Code in child page.
<html>
<head>
<script>
function abc()
{
window.parent.opener.focus();
}
</script>
</head>
<body onLoad="abc()">
child page
</body>
</html>

Related

Rewrite random/button Javascript without jQuery

I’ve this script. It works fine in this situation, but it seems to conflict with other scripts in the site I'm working on. How can I rewrite it that it doesn't need jQuery / OnLoad?
This is what it does: the button opens a random link from an array in a new window everytime you click on it.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script>
<title>TestBase</title>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
var links = [
"http://vetteletters.nl",
"http://todont.co",
"http://planetx.nl/titles"
];
$("#rnd_link").click(function(){
window.open(links[Math.floor((Math.random()*3))]);
});
});//]]>
</script>
</head>
<body>
<button id="rnd_link">Random</button>
</body>
</html>
window.onload = function(){
var links = [
"http://vetteletters.nl",
"http://todont.co",
"http://planetx.nl/titles"
];
var btn = document.getElementById('rnd_link');
btn.onclick = function(){
window.open(links[Math.floor((Math.random()*3))]);
}
}//]]>
Should work.
This works:
<script type='text/javascript'>
var links = [
"http://vetteletters.nl",
"http://todont.co",
"http://planetx.nl/titles"
];
window.addEventListener('load', function() {
document.getElementById("rnd_link").onclick = function() {
window.open(links[Math.floor((Math.random()*3))]);
};
});
</script>
If you just want to avoid using jQuery, I suggest using window.onload to trigger the function when the page loads.
window.onload = function(){
// do stuff
};
Then in order to select a DOM element, you'll use the getElementById() method, then you'll add an event listener for a mouseclick:
document.getElementById("rnd_link").addEventListener("click", function(){
// do stuff
});
Here's the MDN entry for event listeners for further reading: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
I'd suggest this:
Call the function when the button is clicked.
<button id="rnd_link" onclick="random()"></button>
<script>
var links = [
"http://vetteletters.nl",
"http://todont.co",
"http://planetx.nl/titles"
];
function random(){
window.open(links[Math.floor((Math.random()*3))]);
}
</script>
More info for onclick here

In JavaScript, 'onbeforeunload' not working until we clicked on opened window

In JavaScript, 'onbeforeunload' not working until we clicked on opened window.
Please suggest me any solution.
Thanks in advance
Based on your comment, I don't think you're using it correctly. You need to return a string, not use an alert.
Parent page:
<script>
var w;
function x(){
w = window.open('test2.html');
}
</script>
<button onclick="x();">open</button>
<button onclick="w.close();">close</button>
Popup:
<html>
<head>
<script>
window.onbeforeunload=function (e) {
e.returnValue = "Are you sure you want to leave this page?";
return e.returnValue;
}
</script>
</head>
<body>
popup
</body>
</html>

Open window behind the parent window JS or PHP

I'm needing open a window behind parent window, and I'm not finding anything to do that.
Could someone help me with this code or tips?
Parent.php
<script type="text/javascript">
$(document).ready(function () {
window.name = "parent";
$('#link').click(function (event){
event.preventDefault();
window.open('child.php', 'fullscreen=yes', 'scrollbars=auto');
});
});
</script>
<body>
<a id="link" href="/">Open Window </a>
</body>
child.php
<script type="text/javascript">//<![CDATA[
$(document).ready(function () {
$('#link').click(function(event){
event.preventDefault();
console.log(window.opener.location);
var goBack = window.open('', 'parent');
goBack.focus();
});
});//]]>
</script>
<body>
<a id="link" href="#">Return to Parent </a>
</body>
LINK CODE: http://pontodosjogos.com/testegrana.php
You can use this code to display a pop-under window:
<script>
//specify page to pop-under
var popunder="http://yahoo.com";
//specify popunder window features
//set 1 to enable a particular feature, 0 to disable
var winfeatures="width=800,height=510,scrollbars=1,resizable=1,toolbar=1,location=1,menubar=1,status=1,directories=0";
function loadpopunder()
{
win2=window.open(popunder,"",winfeatures);
win2.blur();
window.focus();
}
loadpopunder();
</script>
Source: http://www.javascriptkit.com/script/script2/popunder.shtml
Keep in mind that browsers will likely block the pop-under window and it is generally annoying seeing those from a user's perspective so try to not overdo those.

Trap the JS error of child window using onerror on parent page

I am having a issue with IE, when I open a child window using javascript and attach onerror event on child window I am not getting JS error caught in OnLoad of child window. Below code is working fine in FF.
here is code example:
Parent Page:
<html>
<head>
<script>
function openpage()
{
console.log('opening');
var w = window.open('demo2.html', '_blank');
//without setTimeout onerror is not working
setTimeout(function(){w.onerror = function(msg, file, line) { alert(msg); };}, 3000)
//w.onerror = function(msg, file, line) { alert(msg); };
}
</script>
</head>
<body>
<input type="button" onclick="openpage()" />
</body>
</html>
Child Page code (demo2.html):
<html>
<head>
<script>
//function with error
function fun(var1,var2){aler(var1);}
</script>
</head>
<body onload="fun(2,3)">
<input type="button" onclick="fun(2,3)" />
</body>
</html>
Please suggest a way to capture the JS error generated on child page If above way is not valid/correct.
Thanks,
Pankaj

Capture clicks with parent form from iframe

I have a parent page with an iframe in it. Inside the iframe is a single tag. On the parent page, can I fire an event when that tag is pressed? I know that if I place the javascript that captures the event inside the iframe then it will work, but for my project I need to have the parent page capture it.
This is my code so far.
Parent Code:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var $MyFrame= $("#myiframe");
$MyFrame.on("click", "a", function (e) {
alert("hello");
});
</script>
</head>
<body>
<iframe id="myiframe" height="3500" width="950" src="Iframe page" frameborder="0" style="display: block; margin: 10px auto;"></iframe>
</body>
</html>
This is my page in the iframe:
<html>
<head>
</head>
<body>
Click Me!
</body>
</html>
Is this possible?
var $MyFrame = $("#myiframe");
// You need to wait for the iFrame content to load first
// So, that the click events work properly
$MyFrame.load(function () {
var frameBody = $MyFrame.contents().find('body');
frameBody.find('a').click(function () {
// here is the iframe a click callback
alert("hello");
});
});
Not sure but this could help you with .contents() if frame source is at same origin:
var $MyFrame= $("#myiframe");
$MyFrame.contents().find('a').on("click", function (e) {
alert("hello");
});
and i noticed that you have assigned an id and you are referencing that frame with class.
your myiframe is id and not class.. use id selector #
try this
var $MyFrame= $("#myiframe");
$MyFrame.on("click", "a", function (e) {
alert("hello");
});

Categories