In the below code, I am unable to get properties of window.opener (window.opener properties getting nullified) after window.open function. Any workaround so that I can retrieve window.opener properties after window.open function gets called.
function OpenNewWin(e)
{
var win = window.open('URL', '_blank', 'left=0,top=0,width=300,height=400,toolbar=0,scrollbars=0,status=0,dir=ltr');
// I want to assign window.opener properties to win.opener, but i am unable to do so as window.opener is becoming null.
win.opener = window.opener;
window.focus;
win.focus();
}
Thanks.
Related
I have a button in my homepage that opens to a new window FindAPark.html. I'm trying to pass a variable to that HTML page without GET.
I've tried this in my Homepage.js:
function buttonClick() {
var newWindow = window.open('FindAPark.html');
newWindow.my_special_setting = "Hello World";
}
And then in my FindAPark.js:
window.my_special_setting;
console.log(window.my_special_setting);
The console says it's "undefined" instead of showing "Hello World". What might be the problem, and what might fix it?
EDIT: Now it's telling me "Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame." I haven't hosted these on a domain yet, but I own both HTML files. What do I do?
Use window.postmessage
In the opened window
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
console.log(event)
// ...
}
Now dispatch the event from the parent window
newwindow.postMessage("The user is 'bob' and the password is 'secret'",
"https://secure.example.net");
Use localStorage like this:
function buttonClick() {
localStorage.setItem("mySepcialSetting", "Hello World");
window.location.href = "FindApark.html";
}
Then you can access it as follows:
var mySpecialSetting = localStorage.getItem("mySepcialSetting");
console.log(mySpcialSetting);
Provided your opened window is from the same security domain, you can pass the variable like you did in your question, but you should wait for the new window to completely load.
let newWindow = window.open('FindAPark.html');
newWindow.addEventListener('load', function(){
newWindow.my_special_setting = "Hello World";
}, false);
Another option is to read a variable from the window that opens the new window... same security policies apply as above.
let my_special_setting = window.opener.my_special_setting;
How do I decide whether to open my local web page window with window.open or window.location.href?
Simply,
window.open() will open a new window for your passing URL in side the parentheses.
window.location.href will redirect you to the passing URL with in the same window.
if window.opener == null
// open by window.location.href
else
// open by window.open
I have open a new window from another window. I need to get the parent (opener) windows url. Is there a way to get the window.opener url?
window.opener.location.href is what you want. However, you can only read this if it's in the same domain as the window you are calling it from.
It will be redirect to another page.
window.location = "https://www.w3schools.com"
Visit W3Schools!
document.referrer seems to do the trick
Some tests:
if (window.opener) {
console.log(
window,
window.opener,
window.opener === window,
document.referrer, // Works!
// window.opener.location.href, // crashes
// window.opener.location.toString(), // crashes
// window.opener.document, // crashes
)
}
I'm trying to redirect from child page to parent page with this javascript:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Close", "ClosePopUp();", true);
<script language="javascript" type="text/javascript">
function ClosePopUp() {
window.opener.location= 'ParentPage.aspx';
self.close();
}
</script>
It works with Firefox & Chrome. But not with IE 9.
The error I'm getting is:
Unable to get value of the property 'location': object is null or undefined
alert(window.opener) returns null in IE 9.
After searching for quite a while I have found the solution for internet explorer.
You need to use
window.opener.location.href='';
window.opener is a non-standard property and is not available in all browsers. It will also evaluate to null if the window wasn’t opened from another window, so it seems pretty unreliable.
I think you can use window.open
window.open(URL,name,specs,replace)
More info here
Update
I think I have got it now. Add an eventhandler in your parent window to your child's unload event.
var win = window.open("ChildPage.aspx");
function popUpUnLoaded() {
window.location = "ParentPage.aspx";
}
if (typeof win.attachEvent != "undefined") {
win.attachEvent("onunload", popUpUnLoaded );
} else if (typeof win.addEventListener != "undefined") {
win.addEventListener("unload", popUpUnLoaded, false);
}
This means that when the function below executes your parent page picks up on it.
function ClosePopUp() {
self.close();
}
I have one child popup.
From this child popup I am sending some values from child popup to textbox of parent page.
javascript is working fine in firefox but not working in chrome & IE
Bellow is the javascript
function submitValues(value1,value2)
{
window.close();
window.opener.document.getElementById("value1Id").value = value1;
window.opener.document.getElementById("value2Id").value = value2;
}
I am not able to figure out that what is the problem.
You can pass arguments to showModalDialog function. Simply pass window object as an argument.
window.showModalDialog(theURL, window);
Yo can access the arguments from the modal window using dialogArguments. See: http://msdn.microsoft.com/en-us/library/ms533723%28VS.85%29.aspx
var openerWindow = window.dialogArguments;
Can you try the below function if it works
The window.close will close the window
function submitValues(value1,value2)
{
window.opener.document.getElementById("value1Id").value = value1;
window.opener.document.getElementById("value2Id").value = value2;
window.close();
}