window.name is not persistent in Google Chrome - javascript

I'm seeing a strange behavior in Google Chrome, using the window.name property.
For example:
open a tab and go to http://google.com .
Open up console, and type window.name="hello".
Now in the same tab, go to http://chase.com.
In the console, type window.name.
I expect to see "hello" returned, but instead I see "".
Is this a known issue for Google Chrome? It works for me in FireFox.
Anyone have any insight to this behavior?
Thanks!
Update:
If, instead of typing in a new URL, I type window.location="http://chase.com", then the window.name persists!

window is a global object for each document, not for the browser window. In a page with iframes you will have one window for each iframe for example. Each time a document is loaded, a new global object is created and populated for the context. When the document is unloaded, the global object along with all its data is destroyed.

Chrome might start a new process depending on the site.
I would call what Chrome does perfectly acceptable.
You really shouldn't be dependent on any global variable to be persistent. I would consider using session or local storage.

Related

javascript link window open without extra slash

Let's say I am currently at the following link:
"localhost/admin/test" when i do
window.open("/user/list/2034", "_blank")
it will appear like this:
localhost/admin/test/user/list/2034
what can i do to make it like this instead?
localhost/user/list/2034
I am assuming that windows should be window. If it really is windows then you have a custom object and need to show us what that is before we can answer your question.
The other point to note is that window.open will open a new window, regardless what you name it, so you don't need to use the _blank name. You only need to specify a name if you want to subsequently reuse that window (e.g. open another URL in the same other window).
When at http://localhost/admin/test, if you do:
window.open("/user/list/2034")
It will go to http://localhost/user/list/2034, unless...
window.open has been redefined somewhere. You can do console.log(window.open) and the console should say something like ƒ open() { [native code] } if it hasn't been redefined.
Your web server is responding to /user/list/2034 with a redirect to /admin/test/user/list/2034. The network tab in your developer console will show you the HTTP requests and responses where you can see if the web server is redirecting.
Your link really doesn't start with a / and you actually have window.open("user/list/2034")
You're viewing a cached version of the page with the above error in it, the source code is fixed but the browser hasn't loaded it. Try again in a private browsing window to see if it still happens.
You have some browser plug-in or extension interfering with your page. Try another browser/computer without the extensions and see if it still happens.

window.open why some user's IE open new window every time.

This issue is driving me crazy, I have an app which is referenced via .asp pages and in one of these pages there is a javascript function to open a popup
window.open("popup.aspx", "myPopup","width=300,height=100,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,alwaysLowered=yes,location=no,directories=no,titlebar=no");
Looking up various documentation it has been stated that if the same window name is used then this window will be re-used. This is correct on most user's IE but on an some end users PC it opens an additional popup window even though the same code is being used.
To verify this I created a test.aspx and simply duplicated the open function previously stated. On my PC I got one popup window, on the some users got two. It’s definitely Internet Explorer on this PC because I have installed Firefox and there is no issue and the same window is referenced.
IE(11) version is same all.
What problem??
Save an object reference of the opened window to a global variable and do a validation before calling window.open:
var windowObjRef = null; // global variable
if(windowObjRef== null || windowObjRef.closed)
{
windowObjRef = window.open("popup.aspx", "myPopup","width=300,height=100,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,alwaysLowered=yes,location=no,directories=no,titlebar=no");
}
else
{
windowObjRef.focus();
}

Opera window.open without changing URL

I am using javascript method window.open to get the reference to the other windows of mine service.
The main idea is that if I use window.open('', name ) and window with given name exsists then I get the reference to it (and if it is from same domain I can comunicate with it). If it does not the new window has url 'about:blank'. So if it is about:blank I am closing it.
That works on every browser except Opera... When I am calling window.open with empty string as first argument on every browser if the window with this name exsits I will get refenrence to it and nothing more. But not on Opera - there the URL of this window will be changed to about:blank.
Are there any way to workaround this on Opera?
I'm suggesting window.focus('name'); :)?
I know this is an old question, but there are still Presto Opera users out there, and here's a solution (more of a hack) for it (yes, browser sniffing is required):
var popup = window.open("file:///D:/nonexistent_file", name);
This way you'll get the window reference and a insignificant security exception in new window (if a new window with about:blank url was opened).

IE9 "Can't execute code from freed script" when calling hasOwnProperty()

Here is the scenario:
I have a container page that swaps iFrames in and out to show different content. All iFrames come from the same domain. https is enabled.
The container page has an object called Flow, with functions set/getParameter
The first iFrame, s0-welcome, creates an object, data, and calls Flow.setParameter('data', data);
The container then replaces the first iFrame with a second iFrame, s1-transfer.
The s1-transfer calls Flow.getParameter('data') and stores it in a local variable s1data
In the IE9 debug tools console, if I type s1data it shows me all the properties of that object. However, if I call s1data.hasOwnProperty('prop1'), I get a "Can't execute code from a freed script" error. If I call Object.prototype.hasOwnProperty.call(s1data, "prop1"), everything works fine.
It looks to me that there can be 2 possibilities:
1) Container page holds on to the reference from the first iFrame, but when the first iFrame gets disposed, it loses some of the data. This seems unlikely since the only thing I can't access is functions
2) There is a security restriction that does not allow one iFrame to run code related to another iFrame even if both iFrames are from the same domain.
Any thoughts?
Just ran into a similar issue. For me, simply changing s1data.hasOwnProperty('prop1') into ('prop' in s1data) made the error go away.

window.opener is not right

I'm opening a popup and want to have a click action in it cause the execution of some jQuery in the page that opened it. Everything I find online says I should be able to do that with window.opener, (e.g. JQuery - Write to opener window)
But when I console.log window.opener, it's just 'true', not a real object. window.opener.jQuery is undefined as is window.opener.$ and 'window.opener.document'.
Here's the window open code:
window.open('http://google.com' , "moderatorWindow", 'width=300, height=300');
This is in Safari. Other pages are able to launch a popup and when I inspect window.opener on those, I get a real object. What am I doing wrong?
Your variable is true and not an object because of same-domain policy rules. Just like an iframe, if the popup you open is not on the same domain or sub-domain then it is lost to you after you create it. The web would be a very unsecure place if I could say, open a (hidden) iframe on my site to gmail.com and was able to read your email.
Even if the popup is on a sub-domain you have to do extra work and set the document.domain value of both windows to the root domain (eg. mydomain.com). This is to ensure that the popped-up site wants to be known to its parent (again, think security, if my coke.ning.com community could open a hidden iframe to your pepsi.ning.com and do brute force attempts at a login, etc.)
To prove my point try actually going to google.com and opening up Firebug (or Inspector if you're using Safari or Chrome) and doing:
var bob = window.open('http://google.com' , "moderatorWindow", 'width=300, height=300');
bob.window.location.href; // returns "http://www.google.com/"
Lastly, feel free to call jQuery on the child page to modify elements on the same page and vice-versa but you can't use jQuery from one page to modify the dom of the other page. I tried this a few years ago to try to save on some loading time and, unless something has changed, it doesn't work. jQuery seems to be bound to the window object of where it was created. Weird things happen.
Presumably you are calling:
console.log(window.opener);
which should call the toString() method of whatever window.opener references. It should reference a window object, which is a host object. Per the ECMA-262, a host object's toString() method (if it has one) can return anthing it likes, even throw an error (try it on certain IE host objects implemented using ActiveX).
This article might help: http://developer.apple.com/library/safari/#documentation/AppleApplications/Conceptual/SafariJSProgTopics/Articles/Cross-documentmessaging.html

Categories