How can I get the ID of my Window popup - javascript

How can I get the ID of my Window ?
$(window).unload(function () {
if (alterado == 1) {
window.opener.location.reload();
}
});
How can I get the ID of my window.opener.location ?
I need to put a block in this window like $("#window").block(message:"loading");, or a div that exists in the parent window.

window.opener.$("#window").block(message:"loading");

Your can use window.opener.document to get access to the opener document.
like
var doc = window.opener.document;
doc.getElementById("xyz").innerHTML = "Loading"

Related

Facebook look alike login redirect [duplicate]

This doesn't work as to return the focus to the parent window in Firefox 4 and 5
<html>
<head>
<script type="text/javascript">
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>The new window.</p>");
myWindow.blur();
window.focus();
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
How can I return focus to the parent window using javascript?
You need to give your parent window a name.
Parent.html
<a id="link" href="#">Click to child.html </a>
<script type="text/javascript">
$(document).ready(function () {
window.name = "parent";
$('#link').click(function (event){
event.preventDefault();
window.open("child.html");
});
});
</script>
Child.html
<a id="link" href="#">Return to Parent </a>
<script type="text/javascript">
$(document).ready(function () {
$('#link').click(function(event) {
event.preventDefault();
var goBack = window.open('', 'parent');
goBack.focus();
});
});
</script>
Now, whenever you click the link in child.html, the parent window will be focused.
Sorry for necromancing a very old question, but I ran into the same problem and couldn't find any solution. I eventually solved this by using the following script:
e = window;
while (e.frameElement !== null) {e = e.parent;}
e.parent.focus();
Apparently, calling just window.focus(); isn't enough. You have to call the Window's parent's focus method by using window.parent.focus(); You can't access any other properties from JS from it though, it will give a cross-source error. This script will also work if your script was fired from a frame within a page, assuming the frame and main page share the same source (domain).
I don't think you can return focus, not without closing the child window:
myWindow.close()
I tried to use the above example and it didn't work for me. I had to use a recursive loop to find the opener until the window didn't have an opener (bad design, I know, but our app has several levels of child windows). Here is the code example. I hope it helps:
/* This is for the button functionality.
The button element first comes in.
This element is then used to get the document that has the element.
The document window opener is then passed to the focusMain method */
function setFocusOnMain(el){
var doc = el.ownerDocument;
var win = doc.defaultView || doc.parentWindow;
focusMain(win.opener.document);
}
/* This is a recursive method that checks for a parent window of the current document.
If the document has no window opener, focus on this element because this is the Main.
If the document has a window opener, pass the window opener's document to focusMain. */
function focusMain(doc) {
var win = doc.defaultView || doc.parentWindow;
if (win.opener == null) {
doc.focus();
} else {
focusMain(win.opener.document);
}
}
Here is how I solved this.
const ParentWindowName = 'ParentWindowName';
// here set the parent window name so that you can switch back to it later and open the new window/tab
export const openChildWindow = (id) => {
window.name = ParentWindowName;
window.ChildWindowName = window.open('/child', id);
};
// here you can change the focus back to the new window.
export const focusParentWindow = () => {
window.open('', ParentWindowName).focus();
};
If this is a Window you opened yourself you can use opener.focus();

window is not loading immediately after clicking a link

When I click an anchor link, the current page should be loaded again immediately:
<a href="javascript:void(0);" class="BackToFirst"
onclick="popNav('BackToFirst');">Back</a>
function popNav(type) {
if(type == "BackToFirst") {
$(".first").show();
$(".second").hide();
$('.BackToFirst').click(function() {
document.location.href = window.location.href;
});
}
}
I expect that when a user clicks on the link, the current page will load immediately but it is taking some time to load.
It is unclear what you are trying to do.
show/hide is immediately undone when you reload the page
it is recommended to use location.reload(1) instead of setting the supposedly read-only document.location
you might want to use e.preventDefault instead of the javascript void
Are you absolutely sure this is not an X/Y problem? Can you explain the actual usecase?
var current = sessionStorage.getItem("which"); // does not run in a snippet
current = current ? current.split("|") : []
if (current.length) {
$("." + current[0]).show();
$("." + current[1]).hide();
}
$(".BackToFirst").on("click", function(e) {
e.preventDefault();
$(".first").show();
$(".second").hide();
sessionStorage.setItem("which", "first|second")
setTimeout(function() {
location.reload(1); // are you absolutely sure this is not an X/Y problem?
}, 500); //let the show/hide sink in
});
Back
Your Click handler is only assigning a new click handler to the link, try this which just directly navigates:
function popNav(type){
if(type=="BackToFirst")
{
$(".first").show();
$(".second").hide();
document.location.href = window.location.href;
}
}
I think that you got two concepts mixed up, the above code attaches a DOM event to the link directly on it, the other way would be to use JQuery to attach an event to that button like so:
HTML:
Back
Script:
$('.BackToFirst').click(function(){
$(".first").show();
$(".second").hide();
document.location.href = window.location.href;
});
If you still want to check the type then data attributes are a nice way to go when working with JQuery:
Back
$('.BackToFirst').click(function(){
if($(this).data('linktype') == 'BackToFirst'){
$(".first").show();
$(".second").hide();
document.location.href = window.location.href;
}
});
I think you are overcomplicating your code. You are binding onClick after you click on the element. I think something like this should be better.
HTML:
Back
JS:
function onClickHandler(type){
if(type !== 'BackToFirst') {
return;
}
$(".first").show();
$(".second").hide();
location.reload();
}

How to make "modal conformation" functionality in this function call?

When i m calling this below function in script, i will get a popup dialog box to do some instructions.
Here is my sample code:
function updateStatus(instrxnID){
exporter.fn.childWindow({
instrxnID : instrxnID,
url:'pgks/fund/update/view.page'
},'pgks','Popup',{top:100,height:459,width:884,left:200});
}
exporter.fn.childWindow will call the below function to open popup'
childWindow : function(elements,path,title,setting){
setting = setting != undefined ? setting : {top:100,height:300,width:400,left:200};
var keys = exporter.fn.keys(elements);
var offset = "width="+setting.width+",height="+setting.height+",top="+setting.top+",left="+setting.left;
myWin = open("", "displayWindow", offset+",scrollbars=no,status=no,dependent=yes,directories=no,menubar=no,personalbar=no");
myWin.document.open();
myWin.document.write("<html><head>");
myWin.document.write("</head><body><form name='form' action='"+elements.url+"' type='post'>");
for ( var a = 0; a < keys.length; a++) {
myWin.document.write('<input type="hidden" name="'+keys[a]+'" value="'+elements[keys[a]]+'">');
}
myWin.document.write("</form><script type='text/javascript'>form.method=\'post\';form.submit();</script></body></html>");
myWin.document.close();
}
After finishing those instruction ,i should return to the main page or parent page of this popup.
Note: By clicking background page or another link, this modal should not disapper.
Example: i need to attain something like this example
Attach either a click handler to your submit buttons, or a submit handler to the form. In this handler hide (display: none;) your popup or, if you're using a window object for your pop up, call .close() on the instance.
EDIT:
$(".my-button").on("click", function() {
myWin.close();
});

Click HTML except one element, without using jQuery

I show us the code:
(function (){
var element = document.getElementById('bar'), hideElement = document.getElementById('foo'),
var html = document.getElementsByTagName('html')[0];
tool.onclick = function() {
hideElement.style.display = 'block';
html.onclick = function() {
hideElement.style.display = 'none';
}
}
})();
This piece of code work's fine, but, after clicking html, I can not reopen the hidden element.
I want to click the html element and give display:none to hideElement, then to click the element id="bar", give to the hidden element display:block, but instead of click the element foo, click the html element. What I can do?
Oh, i need help WITHOUT JQUERY, thanks :)
EDIT: something like that : click on body except some other tag not working , but without JQuery,
I'm not sure it's going to answer your question, but here it is: how to handle an event on the body except one element:
document.documentElement.onclick = function(e) {
var evt = e || window.event, // IE...
target = evt.target || evt.srcElement // IE again...
// There, "target" is the element clicked. See where I'm going?
if (target.id !== "foo") {
// Do w/e you want if the page was clicked, except for "foo"
}
}
This is the concept of "event bubbling". You can listen to one element and all its children at once, and get the target as specified in the code up there.
First, you don't appear to be defining tool anywhere that I can see.
Second, you forgot .style in hideElement.display (should be hideElement.style.display).
Third, document.getElementsByTagName('html')[0] is redundant. Just use document.documentElement instead.
Change
html.onclick = function() {
hideElement.display = 'none';
}
to
html.onclick = function() {
hideElement.style.display = 'none';
}

How to return focus to the parent window using javascript?

This doesn't work as to return the focus to the parent window in Firefox 4 and 5
<html>
<head>
<script type="text/javascript">
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>The new window.</p>");
myWindow.blur();
window.focus();
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
How can I return focus to the parent window using javascript?
You need to give your parent window a name.
Parent.html
<a id="link" href="#">Click to child.html </a>
<script type="text/javascript">
$(document).ready(function () {
window.name = "parent";
$('#link').click(function (event){
event.preventDefault();
window.open("child.html");
});
});
</script>
Child.html
<a id="link" href="#">Return to Parent </a>
<script type="text/javascript">
$(document).ready(function () {
$('#link').click(function(event) {
event.preventDefault();
var goBack = window.open('', 'parent');
goBack.focus();
});
});
</script>
Now, whenever you click the link in child.html, the parent window will be focused.
Sorry for necromancing a very old question, but I ran into the same problem and couldn't find any solution. I eventually solved this by using the following script:
e = window;
while (e.frameElement !== null) {e = e.parent;}
e.parent.focus();
Apparently, calling just window.focus(); isn't enough. You have to call the Window's parent's focus method by using window.parent.focus(); You can't access any other properties from JS from it though, it will give a cross-source error. This script will also work if your script was fired from a frame within a page, assuming the frame and main page share the same source (domain).
I don't think you can return focus, not without closing the child window:
myWindow.close()
I tried to use the above example and it didn't work for me. I had to use a recursive loop to find the opener until the window didn't have an opener (bad design, I know, but our app has several levels of child windows). Here is the code example. I hope it helps:
/* This is for the button functionality.
The button element first comes in.
This element is then used to get the document that has the element.
The document window opener is then passed to the focusMain method */
function setFocusOnMain(el){
var doc = el.ownerDocument;
var win = doc.defaultView || doc.parentWindow;
focusMain(win.opener.document);
}
/* This is a recursive method that checks for a parent window of the current document.
If the document has no window opener, focus on this element because this is the Main.
If the document has a window opener, pass the window opener's document to focusMain. */
function focusMain(doc) {
var win = doc.defaultView || doc.parentWindow;
if (win.opener == null) {
doc.focus();
} else {
focusMain(win.opener.document);
}
}
Here is how I solved this.
const ParentWindowName = 'ParentWindowName';
// here set the parent window name so that you can switch back to it later and open the new window/tab
export const openChildWindow = (id) => {
window.name = ParentWindowName;
window.ChildWindowName = window.open('/child', id);
};
// here you can change the focus back to the new window.
export const focusParentWindow = () => {
window.open('', ParentWindowName).focus();
};
If this is a Window you opened yourself you can use opener.focus();

Categories

Resources