I am trying to create some sort of workflow in GreaseMonkey.
I start with GreaseMonkey defining jQuery if it isn't already defined:
/*! jQuery v1.7.1 jquery.com | jquery.org/license */
if (typeof jQuery == 'undefined')
(function(a,b).......function(){return f})})(window); // packed version
jQuery.noConflict();
/*! end of jQuery */
Then, it periodically checks a web page (jQuery.ajax/type=get/url:window.location.href).
When some condition is met, a window is created using
var url = <some page on the same domain>
var opened = window.open(url, "XYZ");
The question is how to get a button on the opened window to click. Let's say on the page there is a button
<input type="button" id="clickme"
I've tried the obvious such as
opened.document.getElementById('clickme').click()
jQuery(opened.document).find('#clickme').click()
But neither work. This is probably a GreaseMonkey issue so would like to see if anyone has something similar working. My current workaround is to set up another GreaseMonkey script against the opened url which clicks the button if window.name = 'XYZ'.
The question is pretty vague; I'm assuming you're creating a window using window.open or something to that effect, and want to interact with the DOM inside.
You should be able to use the reference to the newly-opened window as you would use the window variable in regular JavaScript:
window.document.getElementById(...).doStuff(...)
becomes
var thatWindowReference = window.open(...);
...
thatWindowReference.document.getElementById(...).doStuff(...)
If you've got jQuery loaded inside the window, then you can do
thatWindowReference.$('selector').doStuff(...)
As for how to simulate a click on a button inside that window:
thatWindowReference.document.getElementById('thatButton').click();
or using jQuery:
thatWindowReference.$('#thatButton').click();
Edit: Without using jQuery from inside the new window (test it out on jsFiddle):
var w = window.open('about:blank');
w.document.write('<button id="test" onclick="alert(\'I was clicked\');">Hello</button>');
$(w.document.body).find('#test').click();
Related
Is it possible to refresh a page from another page using Javascript or JQuery without opening the same page in a new tab.
JS:
var newtab = window.open('http://localhost:8081/app/home');
newtab.document.location.reload(true);
I tried the above, but here, it will open a new tab, with the same page, which is already opened in the browser.
Please suggest a method.
I got the idea from a previous Question , here they used window Object Reference to reload the popup window, but for me it wont work, because, the parent window and child window runs in 2 different ports. So using the same trick, what i did is :
HTML:
<a onclick="openNewTab()">app2</a>
<a onclick="refreshExistingTab()">Refresh</a>
JS:
<script>
var childWindow = "";
var newTabUrl="http://localhost:8081/app/home";
function openNewTab(){
childWindow = window.open(newTabUrl);
}
function refreshExistingTab(){
childWindow.location.href=newTabUrl;
}
</script>
refreshExistingTab() this instend of refreshExistingTab
take a look at https://developer.mozilla.org/en-US/docs/Web/API/Window.open
basically if you do window.open and specify a window name it will overwrite that window with the url you provided.
so if you open the page each time with same window name, it should overwrite it each time you do it again from that other page.
I was developing a struts web application, in one of my JSP I created a button to open a new window. The way I did it is:
window.open(location,'_blank');
everything works fine except the new window is always scrolled down to the bottom itself. How to show the top part instead?
I am using Chrome.
Include place this JS at the bottom of the page just before the closing body tag.
<script>window.scrollTo(0,0);</script>
Or you could simply use jQuery if your page is already including it:
<script>$(document).ready(function(){$(window).scrollTop(0);});</script>
Try something like this:
var newWindow = window.open(someUrl, '_blank');
newWindow.onload = function(){
newWindow.scrollTo(0, 0);
}
newWindow.onload();
Try this
window.open(location,'_blank','top=50');
A library I am using gets a reference to the main window by adding this script to a popup window:
var winMain=window.opener;
This script lets the popup window access global variables from the window that opened it. Example:
<select name=MonthSelector onChange="javascript:winMain.Cal.SwitchMth(this.selectedIndex);winMain.RenderCal();">
However this leaves me in an awkward position if I try to call some of these variables without opening a new window. (For instance, if I try to embed one of the calendars inside a div instead of a new window.)
onChange="javascript:Cal.SwitchMth(this.selectedIndex);"
and
var winMain=window;
onChange="javascript:winMain.Cal.SwitchMth(this.selectedIndex);"
Both don't seem to work. Is there some way to get the current window's handle as a variable? Or an I just doing something wrong?
This question appears similar to mine, but the answers don't work.
change it to
var winMain = window.opener || window;
It says is there is no window.opener, use window.
A window.open equivalent is below
var w = window.open('', '', 'width=400,height=400,resizeable,scrollbars');
w.document.write('Content goes here');
w.document.close();
Use it according to your need.
I have the following script:
var a = window.open( "http://sitea.net" );
var b = window.open( "http://siteb.net" );
a.setFocus();
I would expect the focus to go to the first window (sitea.net), but instead the latest child opened is focused (siteb.net). How can I make the first window focus?
I tried the above code. The "window.open" causes both sites to open even without the a.setFocus.
Putting both of the variables within a function should stop the dual loading of the windows.
Here is an example:
http://jsfiddle.net/latoyale/GAKFL/28/
Also I noticed while using your original code that both windows will not open if .setFocus is declared above window.open. Maybe this problem also has something to do with the code positioning of setFocus...
If I open a window using
window.open('myurl.html', 'windowname', 'width=100,height=100');
How do I refer to the new window (from the same page that opened it) using 'windowname'? This question is specifically about this. I'm aware that I could save a reference to the handle by using "var mywin = window.open(...)" but I don't care about that in this situation.
Thanks, - Dave
In firefox (might work in other browsers too, but now it's not my concern) I was able to reference one window accross multiple page loads with
var w = window.open("", "nameofwindow");
This opens new window if it doesn't exist and return reference to existing window if it does exist without changing contents of the window.
With jQuery I was then able to append new content, to make quick collection of interresting links like this
$('body', w.document).append(link_tag);
If you didn't save a reference to the window then there is no way to restore it. However, if that window is still open and if the page loaded there belongs to the same domain as your page, you can run JavaScript code in it:
window.open("javascript:doSomething()", "windowname");
Whether that's sufficient in your scenario depends on what you are trying to achieve.
Petr is correct:
var w = window.open("", "nameofwindow");
works in all browsers, I am using it to retrieve the reference to the window object previously opened by a different page. The only problem is the initial opening of the page, if the popup does not exist, you will get a new window with a blank page.
I tried invoking a Javascript function inside the context of the other document in order to check whether I opened a new window or retrieved the already active page. If the check fails, I just invoke window.open again to actually load my popup content:
var w = window.open("http://mydomain.com/myPopup", "nameofwindow");
Hope that helps.
It is not possible. The windowName is just to be used in target="..." of links/forms or to use the same name again in another window.open call to open a new url in that window.
Try open that window with the name, but URL is '' again, to check if it's a blank window or not. If it's in open, then you will get the window; if not, a new window open, and you need close it.
Add the children in localStorage will help to prevent to open the new blank window.
Please check my code in https://github.com/goldentom66/ParentChildWindow
Sorry I am posting late, but if you still have the other window open, and they are on the same domain, you can run, on the first window:
function getReference(w) {
console.log('Hello from', w);
}
And on the second window:
window.opener.getReference(window);
afaik there's no way like windows['windowname'].
The 'windowname' assigned in window.open() can be addressed as a target in <a target="windowname" [...] >