Why is window.open() loading the URL in the same window? - javascript

I read a book about javascript and it says that when we create a function for opening a new window, the reference to the new window object is stored inside the variable assigned to the window.open(theURL , newWindow) call. It also says that if the same variable is used for two or more URLs, no matter which URL I click it would be open in the same window. However, if I create two variables for two window.open() calls, both sites will open in different (new) windows.
Due to curiosity, I tried to use two different variables, but still when I click both links they are still loaded in the same window. How come? I've tried it on Firefox, IE and Chrome to no avail.

The book is partly correct, however what you actually need is two different values, not really two different variables. If you have two variables with the same value, the result is the same as using the same variable. It's the value that matters, not what variable you use to supply the value.
The second parameter to the open method is the target. If you use a value like for example 'win42', that will be used as the name for the window. If you call open again with the same value for target, it will open in the same window.
You can use the special value '_blank' as target to always open a new window.

Related

How to pass values from HTML webresource to javascript on window close MSCRM

I am opening HTML webresource using Xrm.Navigation.openWebResource but on closing of HTML window I want to pass values from HTML to javascript file from where it is opened. Is there call back function can be implemented?
If I open HTML window using window.open I can call parent javascript function using window.opener.functionname on close but click but I want to know how I can pass values to parent javascript file on close button click of HTML window.
I tried with window.parent.opener.Functionname() but it is not working - getting functionname is undefined but it is defined in parent javascript. Pls suggest.
If you're using the 'old' (as it not the unified interface) user interface with turboforms enabled then the parents javascript is actually in a extra iframe called customScriptFrame, and not on the parent itself.
To call something on the parent you can use
parent.customScriptsFrame.functionname() for IE
and
parent.customScriptsFrame.contentWindow.functionname() on chrome.
On the unified interface its much the same, but far more troublesome.
Now the scripts are in a iframe called ClientApiFrame_[n] where [n] is some random number. And i haven't found a good way to determin that number ahead of time from a webresource.
You could go over all frames of the parent in javascript (parent.frames) to find one that has a id that starts with ClientApiFrame_ but that will throw errors trying to read frames with sources set to external domains, and i dont think is very good practice.
Another possibility is registering the function you want to call with the parent ahead of time. so in the main javascript use this.
parent.functionname = functionname
And then from the webResource you can use the normal
parent.functionname
If the webresource is embedded in the form, then use window.parent
If you Xrm.Navigation.openWebResource to open it, then use window.opener

How to fix 'Cannot read property 'document' of null' when trying to access window.opener.document

I'm trying to access elements in the window that opened the popup however, as the title says, I'm getting 'Cannot read property 'document' of null' when trying to access them.
I've seen a few other posts on this subject however unfortunately none of them helped in this case.
I've tried
openerWindow = window.opener.document;
selectedTableRow = openerWindow.querySelectorAll(".highlighted-rows");
As well as
selectedTableRow = window.parent.document.querySelectorAll(".highlighted-rows");
The above does run, however it brings back an empty array, even though I know that there are table rows with the class .highlighted-rows
And
selectedTableRow = window.opener.document.querySelectorAll(".highlighted-rows");
The interesting thing is, when I try running selectedTableRow = window.opener.document.querySelectorAll(".highlighted-rows"); through the Chrome console, it actually works and returns the row I was expecting, it's just running it through the code that fails.
You will only have an opener if it was set. See docs for more details, but the important bit:
When a window is opened from another window (using Window.open or a link with its target attribute set), it maintains a reference to that first window as window.opener. If the current window has no opener, this method returns null.
If you're sure the window you're trying to access should have an opener, can you post the code that spawned it?

How can we a get a tab element when the document is known?

Before explaining the question, I want to explain my main goal (If there is a better way than my approach):
I have the document element available with me and ideally I wanted to get a browser element such that it identifies a tab uniquely. In my previous approach I used
gBrowser.getBrowserForDocument(doc);
This returned the browser which was indeed unique to the tab (in the sense that attributes stored in it persisted across pages).
If instead, I don't store the browser element, and after moving to another page in the same tab I try the above command again, then the browser is no longer the same one as before (in the sense that it has lost all the stored attributes).
Therein lies my main problem. I want to get hold of the tab browser which I am able to refer to using different documents loaded in the same tab.
I read about a similar function:
gBrowser.getBrowserForTab(tab);
I have a feeling this might work. But again, I am not able to understand where I can get the parameter "tab" from (given a document).
Note: I am using GWT for the development of the extension
EDIT: To clarify the intent of the question, here's the use case as well as my approach:
In my extension, I am interested in monitoring user behaviour on particular websites. In a way it can be thought of as a session which remains active until the user stays on the same website. During the session, I am often required to store various attributes specific to user behaviour. One of the attributes concerning the question in "isSessionActive":"Y" or "" (blank string stands for no)
To make the code more optimal, I do not instantiate a browser for all the tabs in the beginning. Instead, I wait for the cue using an onLoad function. : if a relevant website is visited
Once that happens, I make a call to get the browser using the current document element, see if it has a non empty value for the attribute isSessionActive. If it does not, I set the attributes value to "Y" and instantiate my class which handles the profiling after that.
If it has value "Y", I know that the session is still active and that I don't need to initialize.
The problem which I'm facing is that after the first instantiation, when I move to another page within the same tab, I expected that the call to
gBrowser.getBrowserforDocument(doc);
would get me the browser instantiated previously since it is basically the same tab.
This is not happening. Each time I get a new Browser instance which does not have the attribute isSessionActive as "Y" (probably because the new page has a new document element). Thus, at present all my code instantiates over and over again which is what I do not want.
If you're only working with the current tab (and not any background tabs), then you could just use gBrowser.selectedTab https://developer.mozilla.org/en/XUL/tabbrowser#p-selectedTab

How to copy watched javascript variable in Chrome?

I put a breakpoint in javascript and am testing some code with Chrome. I also added a watch expression for the value.
Chrome breaks at the breakpoint and shows the value. However the value is very long, and it doesn't display it all. I move the window separator to the left but it stops at mid screen. When I double click on the watched variable it wants to edit the expression. When I single click and drag on it, it selects the visible text, but not all. Right clicking does nothing.
This is what I see
url: "m=mtgoxUSD&SubmitButton=Draw&r=&i=&c=0&s=&e=&Prev=&Next=&t=S&b=&a1=&m1=10&a2=&m2=25&x=0...
I want to copy the whole expression without the ... in the end. How can I do that?
I'm adding a late answer after nearly 3 years because with the current Chrome Dev Tools, neither approach work if you have an Array or even just a nested Object property in that variable, following both answers you'll just end up copying a string with a lot of Array[size] or Object strings interleaved in the actual object value, completely useless for complex object hierarchies.
The suggested approaches are ok if you just need to manually navigate through the value but not if you need to copy it as requested in the question.
What i recommend instead, especially if you need to copy the watched value to use it as the content of a new variable, is to dump it to console after it has been stringified.
Show the Javascript console and type:
console.log(JSON.stringify(my_watched_var))
This way the complete structure will be displayed in pure Javascript, a fully reusable/copyable way.
Chrome DevTools' console command-line has a built-in "copy" function:
copy(my_variable)
If the value of my_variable is not a string, it will automatically be converted to JSON. The resulting string is left on the system clipboard for pasting.
Here's the reference doc.
Show the console, then type the expression to be displayed and press . You'll see whole value and you'll be able to select and copy it.
While the debugger is paused, this works even with expressions that involve local variables that are in scope at the current point of execution.
Here's what I do.
Edit: This works in the Scope Variables which is below the Watch Expressions panel.
Double click on the value to switch to edit mode.
Ctrl+A (windows) or Cmd+A (mac) to select the entire value.
Ctrl+C (or Cmd+C) to copy.
Look this answer "Is there a way to auto expand objects in Chrome Dev Tools?" , it iterates trough all the properties of an object and it shows the full hierarchy including datatype and values.
Useful if you need to compare two states of an application.
This link has amazing descripion: https://scottwhittaker.net/chrome-devtools/2016/02/29/chrome-devtools-copy-object.html
Steps:
1) Right click variable and select "Add as Global Variable"
2) In the console, write copy(temp1)
3) Open any editor and paste

Javascript - meaning of "parent.parent.someFunction();"

In a webapplication I came across, there is a javascript line in logout.jsp as:
parent.parent.renderProcessingTextOff();
On debugging the page with IE script debugger, it is breaking on above line with error as:
Object doesn't support this property or method
What is the meaning of this error?
How to solve it?
I am new to javascript, so please explain in simple terms.
Flow of logout is:
on click of logout button, a command is passed and intercepted in interceptor, which directs it to appropriate processor, which forwards the request to logout.jsp.
Its a spring application, if that information is of any help.
Thanks for reading!!
Are there frames or iframes on your page? Or object elements?
parent is a property of the window object (and the window. part of window.parent is assumed if you leave it out).
From MDN:
When a window is loaded in an <iframe>, <object>, or <frame>, its parent is the window with the element embedding the window.
So if from an iframe that is at least two levels down, the line parent.parent.renderProcessingTextOff(); says to call the renderProcessingTextOff() function defined by its grandparent.
Obviously if that line of code appeared where there wasn't a grandparent with that function defined then you'll get the error you quoted.
In this case, it looks like the first word, parent is an object instance. That object has a property called parent, and to access it you use the syntax parent.parent. The property is in turn another object, which is supposed to have a property called renderProcessingTextOff which is called as a function.
If I would venture to guess about your problem, it seems that parent.parent is of the top level Object class, and Object does not have that method.
If you already is using the IE script debugger, put a breakpoint at that line and examine the parent and parent.parent objects.

Categories