This question already has answers here:
Multiple Windows using window.open()
(6 answers)
Closed 5 years ago.
I am trying to open two different windows with the click of a button, but when i click the button it opens the same window twice. This is my function:
function flush(form) {
var custid = form.custid.value;
var url1 = "https://www.blabla.com?type=customer&id="+custid;
flushWindow1 = window.open(url1);
var url2 = "https://web.blabla.com?type=customer&id="+custid;
flushWindow2 = window.open(url2);
}
Does anyone know how I can do this?
From : JSfiddle Two windows on a Click
$('document').ready(function(){
$('input').click(function(){
window.open("https://www.google.com.pk/search?q=123&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a")
window.open("https://www.google.com.pk/search?q=abc&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a")
});
});
try open in new window/tab by add parameter '_blank'
and then focus the window you want by place window.focus(); behind window you want to focus after open two new window
function flush(form) {
var custid = form.custid.value;
var url1 = "https://www.blabla.com?type=customer&id="+custid;
flushWindow1 = window.open(url1, '_blank');
var url2 = "https://web.blabla.com?type=customer&id="+custid;
flushWindow2 = window.open(url2, '_blank');
window.focus(); // focus on url2
}
Try providing second argument to window.open() method, which is the name of the window, if name is different then same url will open in different windows. e.g.
function flush(form) {
var custid = form.custid.value;
var url1 = "https://www.blabla.com?type=customer&id="+custid;
flushWindow1 = window.open(url1,"flushWindow1");
var url2 = "https://web.blabla.com?type=customer&id="+custid;
flushWindow2 = window.open(url2,"flushWindow2");
}
Demo
Related
I dynamically create a link to a. 'mywebsite' is a variable. Everything works but the mywebsite page replaces that of the code. I want to open mywebsite in another page.
May I have a help to solve the problem ?
var mylink = document.createElement("a");
var textForButton = document.createTextNode("More details");
mylink.appendChild(textForButton);
mylink.setAttribute("href", mywebsite);
document.body.appendChild(mylink);
You should set the linke target as this:
mylink.setAttribute("target", '_blank');
Your code my be something like this
<script>
window.onload = function () {
let mywebsite = "http://www.google.com";
var mylink = document.createElement("a");
var textForButton = document.createTextNode("More details");
mylink.appendChild(textForButton);
mylink.setAttribute("href", mywebsite);
mylink.setAttribute("target", '_blank');
document.body.appendChild(mylink);
};
</script>
You can set target=_blank attribute to do this.
mylink.setAttribute("target", "_blank");
So that once you click on the link that you have generated it opens in new tab.
function input_check() {
var num = $("#num").val();
var sys = $("#sys").val();
var tp = $("#tp").val();
var urlstring1 = "";
var urlstring2 = "";
if (sys === 'QA') {
if (tp === 'ACOs') {
urlstring1 = "http://stackoverflow.com";
urlstring2 = "http://google.ca";
window.open(urlstring2);
window.open(urlstring1);
}
else {
console.log(0)
};
};
};
$(document).ready(function() {
$('#checkBtn').on('click', input_check);
});
I want Stackoverflow and Google open, but
when sys === 'QA' and tp === 'ACOs', Google opens but Stackoverflow not.
if I change the window.open order, the first page will open but second page not.
For example,
window.open(urlstring1);
window.open(urlstring2); // the urlstring2 and urlstring1 change order. Now it only opens the urlstring1.
This will be due to your browsers popup blocker, which usually only allows on popup per user action.
According to MDN Reference Window.open() , you also probably need to add a target:
window.open(urlstring1, "target1");
window.open(urlstring2, "target2");
Ok I know this is going to sound weird but it is what the client wants. I am working within a popup and when a user clicks on a datagrid cell within a certain column it will popup a html table with data. I have the second popup to display but it does not get focus. This is currently the code I am using to create the second popup. Any help to get the focus on this second popup would be great.
function onCellClick() {
var cmGrid = igtbl_getGridById("countermeasureDetailsGrid");
var cmCellID = cmGrid.ActiveCell.split("_");
if (cmCellID[3] === "3") {
var countermeasureID = igtbl_getCellById("countermeasureDetailsGrid_rc_" + cmCellID[2] + "_0").getValue();
var recordType = igtbl_getCellById("countermeasureDetailsGrid_rc_" + cmCellID[2] + "_4").getValue();
_crfPopupWindow = new crfPopupWindow(countermeasureID, recordType);
_crfPopupWindow.open();
_crfPopupWindow.focus();
}
}
function crfPopupWindow(countermeasureID, recordType) {
var crfPopup = new WindowDef();
crfPopup.target = "CRF_Popup.aspx?countermeasureID=" + countermeasureID + "&" + "recordType=" + recordType;
crfPopup.windowName = "CRFPopup";
crfPopup.toolBar = "no";
crfPopup.resizable = "yes";
crfPopup.scrollbars = "yes";
crfPopup.location = "yes";
crfPopup.width = 350;
crfPopup.height = 400;
return crfPopup;
}
EDIT: Solution
<script type="text/javascript" language="javascript">
function init() {
window.focus();
}
</script>
Have you checked that the 2nd popup has higher z-index CSS property?
First popup can have z-index of, say, 1000, but the second should have then 1001.
window.focus on page load This works
I am trying to open a new window from javascript but nothing is being inserted into the html:
var callScriptText = $('#callScriptText').html();
var url = '/Action/CallScript/?callScript=';
// Open the current call script in a new window
var openWindow = window.open(url, 'callScriptPopup', 'width = 500, height = 500');
$(openWindow).html(callScriptText);
Does anyone know why?
Here's an example to open a new window with content using jQuery
<script>
function nWin() {
var w = window.open();
var html = $("#toNewWindow").html();
$(w.document.body).html(html);
}
$(function() {
$("a#print").click(nWin);
});
</script>
<div id="toNewWindow">
<p>Your content here</p>
</div>
Open
EDIT:
For those who say that this code doesn't work, here's a jsfiddle to try it http://jsfiddle.net/8dXvt/
Try this:
var x=window.open();
x.document.open();
x.document.write('content');
x.document.close();
I find it works in Chrome and IE.
Building upon #Emre's answer.
With javascript, you can chain, so I just modified the code to:
var x=window.open();
x.document.open().write('content');
x.close();
Also, to force it to a new window (not a new tab), give the first line dimensions. But it has to be the third argument. So change the first line to:
var x=window.open('','','width=600, height=600');
try:
var x = window.open('', '', 'location=no,toolbar=0');
x.document.body.innerHTML = 'content';
var codeContents = $("#contentsfornewWindow").html()
var win = window.open('', 'title', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=1000,left=200,top=70');
win.document.body.innerHTML = codeContents;
VS:
win.document.write(codeContents);
I have notice if there are iframes like youtubes videos , win.document.write loads the iframes where as document.body.innerHTML does not!
I need help of getting this piece of code to work
1) I want to automatically start a media player when a pop-up is open, by changing is URL params, that the pop-up has
2) When the user clicks on on the link I want to replace the old URL with the one the new one clicked. Try using location.replace just ends up being a constant loop.
Not having much luck with the code can anyone help????
var vidlink = $('.link').attr('href');
var autoplay = $('.link:first').attr('href');
var myurl = window.location.href,
paramsStr = autoplay,
paramsObj = {};
var newUrl = $.param.querystring(myurl, paramsStr );
if(window.location.href != newUrl){
location.replace(newUrl);
}
$('.link').click(function(){
loadPlayer(vidlink);
});
Open Player
player another show <a href='?v=53&a=false' class='link'>Ep2</a>
try windowname.document.location = newURL (where windoname is the name of the pop-up)