Get title of child window after it loads fully - javascript

I need to detect changes in the title of a window for OAuth.
I have a link that opens the authentication page in a new window, and I'm just trying to pull the title data. However, for some reason the page doesn't load until all my code executes so the title it finds is just an empty string.
$("#signon").on("click", function () {
var url = $(this).data('authurl');
var handle = window.open(url);
var title = window.document.title;
var title2 = handle.document.title;
});
title is fine, but title2 is null and the handle window hasn't loaded.
I've tried .ready, .onload, .load, and an eventListener, but none of them made a difference.
Why isn't the child window loading any data until this code completes?

Related

How to trigger iframe load event more than once when changing iframe source

I have an iframe that links to another internal web application.
There's a side panel with a list of links that changes the src of the iframe. Sometimes if there's a lot of data, the iframe site takes a while to load. I want to put a spinner icon when a link is clicked and hide it when the frame is loaded.
I change the src using $('#myiframe').attr('src', urlVar) in a click function for the links. I can show the spinner on click.
The problem is, how do I hide it? How do I find out that the iframe has finished loading?
I tried using $('#myiframe').load(function() { }) but that only works on the initial load (i.e. for the first link I click), not for subsequent loads (if I click on another link).
This javascript works for me :
function loadNewUrl (url){
if(url === undefined) url = 'http://example.com?v=' + Math.random();
var ifr = document.getElementById('myiframe');
ifr.setAttribute('src',url);
ifr.onload = function() {
alert('loaded');
};
}

How to set the title for the new browser tab?

I have a question about the new tab for the link.
Is there anyway I can set the browser tab title before user clicks a link? It seems like there is no way to debate the title for the new tab if the html contained in the new tab doesn't have title attribute. Am I right? How do I set the title?
//the href is dynamic so I can't set them one by one because I have 100+ html file here
<a href="test.html" target="_blank">open me<a>
As you have it, this is not possible because your links are just normal HTML links. When the new page opens in a new tab, the current page will not have any reference to it and so cannot change it in any way. You will need to open the page using javascript and set the title that way.
You can dynamically set this up in window onload to find all a tags and add a click event whihc opens the window and sets the title.
If you want different titles for each page, you can store this in a data- attribute in the a tag.
Note tho that this will only work with pages in the same domain (for security), and that it does not handle people right clicking and pressing "Open in New Window". Middle click in Windows does seem to work however.
HTML
open me
JavaScript
window.addEventListener("load", function() {
// does the actual opening
function openWindow(event) {
event = event || window.event;
// find the url and title to set
var href = this.getAttribute("href");
var newTitle = this.getAttribute("data-title");
// or if you work the title out some other way...
// var newTitle = "Some constant string";
// open the window
var newWin = window.open(href, "_blank");
// add a load listener to the window so that the title gets changed on page load
newWin.addEventListener("load", function() {
newWin.document.title = newTitle;
});
// stop the default `a` link or you will get 2 new windows!
event.returnValue = false;
}
// find all a tags opening in a new window
var links = document.querySelectorAll("a[target=_blank][data-title]");
// or this if you don't want to store custom titles with each link
//var links = document.querySelectorAll("a[target=_blank]");
// add a click event for each so we can do our own thing
for(var i = 0; i < links.length; i++) {
links[i].addEventListener("click", openWindow.bind(links[i]));
}
});
Sample JsFiddle
You can pass the title with hash and get it on another page, if this another page is yours and you can modify its code.
1st page:
...
<a href="test.html#the_title_you_want" target="_blank">open me<a>
...
2nd page - modify the body opening tag like this:
<body onload="document.title=window.location.hash.replace('#','');">
If the page you are linking to isn't yours, you can use window.open method:
open me
I have not seen addEventListener work reliably, especially when opening a new page using javascript. The best way to change the tab title and have it work reliably is to set a timeout until the page loads. You may have to play with the timeout value, but it works.
var newWindow = window.open(url, '_blank');
setTimeout(function () {
newWindow.document.title = "My Tab Name";
}, 100);
You have two options. Using pure HTML, you can let the user open up links, then later on change the title. Or you can change the title with inline JavaScript. Here's how you do both:
Method 1
Change your links by assigning a target attribute, and then later on use that window name to control the document. For instance in your links it would be: <a href="whatever" target="theNewWindow">. Whenever you want to change the title for this page, you'd use JavaScript as such: window.open("", "theNewWindow").document.title = "New Page Title!"; The problem with this method however is that all links with that target/window name will open in that same window. In addition, after the first time the link is clicked, your browser won't automatically switch to the new tab/window.
Method 2
Change your links by assigning an onclick attribute, which would open the link manually and change the title of the page immediately. Basically it would come down to look like: <a href="whatever" onclick="var w=window.open(this.href, '_blank'); (w.onload=function(){w.document.title='New Page Title!';})(); return false;">. This opens the window based on the href attribute, immediately changes the title, and sets the window to change the title to that when it finishes loading (just in case there really was a title tag).
The problem with both of these methods (as mentioned by others) is your html files have to be on the same domain.
The simplest way is a follows:
var winTab = window.open("", "_blank")
//Open URL by writing iframe with given URL
winTab.document.write("write iframe with your url in src here")
//Set Title for the new tab
winTab.document.title = "Form Title"
You could make your own Page 2 that opens up the other pages (the ones you can't edit), in a frameset. You can then either change the title dynamically when loading your page 2, or as others have suggested if you use window.open you can control the title from the parent page.
If you are in page 1, and opening page 2 in a new tab, you can't set title for page 2 from page 1.
If you have access to page 2 then it's possible, otherwise not.

passing between windows and writing

I have written a script that opens a pop up window , searches for a user , when finds
the user the search window closes and then the script shows the new user in the parent window.
Basically I tried all of the script separately but I have problems when I pass between windows. The code ahead does not pass the found user, but passes one example user just for meantime to debug the code.
Here is the script file: ( I will explain it later )
var orgWindow
var searchWindow
$(document).ready(function(){
$(".add_participants").on("click", function() {
var orgWindow = window.self
var searchWindow = window.open("http://localhost:3000/users/search", "_blank",
"left=200, top=100, height=300, width=300, menubar=yes");
});
});
function printUser(){
var a = orgWindow.document.createElement("a");
a.setAttribute("href","/users/show");
a.setAttribute("id","3");
var aText = orgWindow.document.createTextNode("elad bezalel");
var img = orgWindow.document.createElement("img");
img.setAttribute("alt","El4");
img.setAttribute("src","/uploads/user/image/3/el4.jpg");
img.setAttribute("height","150");
img.setAttribute("width","150");
myDiv = orgWindow.document.getElementById("par");
a.appendChild(img);
var brk = orgWindow.document.createElement("br");
a.appendChild(brk);
a.appendChild(aText);
myDiv.appendChild(a);
}
function add_user(){
user_id = $("#button").data("user_id");
window.close(searchWindow);
window.blur(searchWindow);
window.focus(orgWindow);
orgWindow.document.write(printUser());
orgWindow.close();
}
I am using jQuery. when I click a button the search window open. Then I find a user in the search window and click on another button (add user) and then the search window closes and the parent window gets focused. Now I tried to implement the user image and name, but somehow it doesn't work.
How should I repair my script?
Note that you have declared orgWindow and searchWindow as variables in two different scopes, the global scope (the two var usages at the top) and local to the click handler. This could be part of your problem.
I think the two separate window contexts are getting intermingled.
In the main page add_user function:
function add_user(user_id){
window.close(searchWindow);
window.blur(searchWindow);
window.focus(orgWindow);
orgWindow.document.write(printUser());
orgWindow.close();
}
And in the popup:
$("#button").click($(window.opener).add_user($(this).data("user_id")));

Getting data of one window into popup window

I want to send data from one window to another. In onclick event this bit of code gets triggered:
var _popup;
if (!_popup) {
_popup = window.open("/popup/index.html", "Keywords Source Table", "height=700, width=800");
} else {
_popup.location.reload();
_popup.focus();
}
_popup.dataObj = data;
Then in other window I get this data with window.dataObj.
My problem is that I don't want to open a new window every time the click event gets triggered, but update the content of popup if it is already open. Initially it loads fine in initial open, but when I try to reload the content of already opened popup window with new data, the data is displayed as undefined.
Var x = window.open("");
x.passDataFuntion();
just have a global function in your popups JavaScript that you call from your child.

Close child window once loaded in jQuery

I'm trying to write a JS/jQuery script that opens a whole load of links from a page in child windows, then closes each child window once it has loaded. This is because once the child page has loaded, it triggers a download, and I then need the child page to close. I've tried a variety of things, but none of them seem to work, perhaps someone can help me?
Here's an example of something I was trying:
var imgURL = "http://...etc";
var imgWindow = window.open(imgURL);
imgWindow.ready(function(){
this.close();
});
Would really appreciate any suggestions how to get this to work, bit stuck!
The below code will open a new window and closes after that window is loaded.
var imgURL = "http://yourdomain.com/imagePage.html";
var imgWindow = window.open(imgURL);
imgWindow.onload = function(){
imgWindow.close();
};
This will work only if the child window is from the same domain. If the URL of the child window is not the same domain, you can't do anything. That would be a XSS vulnerability.
The main problem with your code above is that there is not ready method for a window object.
Try this :
var imgURL = "http://google.com";
var imgWindow = window.open(imgURL);
imgWindow.addEventListener('load', imgWindow.close(), true);

Categories