简体   繁体   中英

js function to open in new window or tab

I have a date picker php page and a button which goes to this function

function clickme()
{
if (checkEmpty("date")) return;
if (checkEmpty("end")) return;

var d = convertDate(EL("date").value);
var e = convertDate(EL("end").value);
if (d == null) {
    EL("date").focus();
alert("The date must be in dd/mm/yyyy format.");
return;
}
if (e == null) {
    EL("end").focus();
alert("The date must be in dd/mm/yyyy format.");
return;
}

var x = getXmlHttpRequest();
if (x == null) {
    alert("Unable to get XmlHttpRequest");
    return;
}

var u = "dates-xl.php?date="+ d +"&end=" + e;

x.open("GET", u, false);
x.send();

var t = x.responseText;
if (t != null && t != "") {
    var e = EL("content");
    e.innerHTML = t;
}
}

This works, but opens in the same page which has a header footer etc, I need it to open in a new tab or window, how can I do this?

Have a look at something similar to the following:

var u = "dates-xl.php?date="+ d +"&end=" + e;

x.open("GET", u, false);
x.send();

var t = x.responseText;

var win = window.open("", "_blank"); // This will only work if this being called as a result of a user action (e.g. a button click).
win.document.body.innerHTML = t;

Have a look here for further information:

https://stackoverflow.com/a/19078591/3713362

https://www.w3schools.com/jsref/met_win_open.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM