how to remove body onload function in java script? - javascript

I met one critical problem in java script .....help me to fix this.......
i have written the onload function in one jsp page say login.jsp...
in tat function i used window.open method to open a new window again with the same jsp page login.jsp with disabling the toolbars......
now wat happening is when iam opening same page again in new window obviously tat body onload function will again get called and opens a new window in indefinite........
but what i want is, i have to remove that onload function in tat jsp page once a new window is opened..
Is it possible to remove tat onload function while getting opened in the new window??
could anyone please come up with an idea or little bit of code to do this using java script??

Since it's the same page a quick workaround could be to check if the current window has been opened programmatically, before executing window.open, something like this:
window.onload = function () {
if (!window.opener) {
window.open(/*...*/);
}
};
The above code checks if the window.opener property has a value.
This property contains a reference to the window that opened this current window, and of course if the current window hasn't been opened programmatically, it will contain null.
In conclusion, the window.open method will be invoked only once.

It seems you're going about this the wrong way.
Although there may be a way to intercept the onload function before it actually runs (some javascript libraries can help you to add a handler to the OnDocumentReady event), but maybe you should be doing something serverside. For example - If you want NOT to popup the window in the second window, then one way is to set a querystring parameter telling the server not to add that attribute to the body tag.
You could also check the referrer. If the user is coming from that page, then don't add the attribute to the body tag.

Try having the popup JSP look to see whether you're coming from the problematic page. If so, set window.onload = null -- better yet, don't set anything at all in that JSP.

Related

access dynamically added element on parent window

I need to get information from parent window on a new window. For that I use--
$("#mains").html($(window.opener.document).find(".dqe").html());
Or
$("#mains").html($(".dqe", window.opener.document).html());
It works good. But if I have some elements that are dynamically added on the parent window, the new window doesn't get that part.
Can anyone please help??
Your function only loads the data from the parent window when it is called.
To keep the document in sync you would either need to poll for new data on the other window (run your code again at an interval). You could also send a message to the new window when changes occur using postMessage() (https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage).
Off course these are just some options, bottom line is the second window won't know when it should update unless you tell it when to.
Good luck!

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

child window onfocusout event not firing on refreshing

I have one main window and some child windows. My child window is creating by code behind.
var newWindow = window.open(url, windowName, windowFeatures);
Also I need to add
newWindow.document.onfocusout = this.windowOnUnload;
event to the child. My problem is that, when I need a POST action to this page I again recreate this child window with the same code stated above. But the onfocusout event is not firing. But first time it will fire. Also I am creating the child window is always with the same window name. Please help me finding a solution. Thank you
When you recareate the window, did you mean update its contents with an ajax call? or do you literally close it and then again call window. open, and finally wire the onfocusout event?
If the former is the case, then you will need to rewire the event to the new document that has been obtained using the AJAX call, as the whole document is changed and the previous event handlers for the previous document object are no longer relevant.
Update: could you post all the relevant code that you are using? This is how I can best help.
newWindow.document.onfocusout = this.windowOnUnload; doesn't seem to be the correct way to do what you need. I'm surprised it's working with you. Check this question: Is there a way to detect if a browser window is not currently active?

Add method to javascript's Window object before the page starts loading

Is it possible to add a method to a dynamically created iframe's Window object before the requested page starts loading? I would expect to do this though iframe.contentWindow.myMethod = function() { }, however iframe.contentWindow is NULL before the page loads.
I can add the method when the onload event fires, however some pages make an inline call to the method I'm adding. As the browser executes code when received 'method doesn't exist' errors are being raised.
I'm hoping there is a point I can get access to the window object as soon as it is created, before the content is downloaded and processed?
I'm hoping there is a point I can get access to the window object as soon as it is created, before the content is downloaded and processed?
I very much doubt there is. Perhaps you could modify the pages being loaded so that they call the method in the parent window instead, where you know you'll already have the method defined. E.g., instead of them doing:
someMethod("some arg");
they'd do
parent.someMethod("some arg");
...where someMethod is a global function within the opening window.
Live Example | Source | Source of iframe
You can't modify a page before it starts loading - there's nothing there to modify. You can modify it while it's loading - using a <script> tag at the top of <head> that executes directly, rather than waiting until onload. That's about the soonest you can do it, though.

Execute JS on dialog onload

I would like to execute some JS function each time my YUI dialog gets loaded. I can't do that from body onload because that body tag belongs to parent page. I tried doing that by adding the function on onContentReady event. But that works only when the dialog gets loaded for the first time. Then if I close the dialog and reopen it, it doesn't work; probably because the content is already ready when the dialog was opened for the first time and hence the function is not called this time.
Any idea what can be done to solve this?
I decided to use dialog.changeContentEvent.subscribe to register the JS code I wanted to execute on dialog onload. This isn't exactly equivalent to onload but still suits my requirement.

Categories