Pass data from pop-up window back to javascript - javascript

I have a window.open function in javascript that calls a page. I then want to grab the data from that page and return it to the javascript function.
Can anyone help me do this?
$("#popup").click(function(e) {
var url = $(this).attr('href');
e.preventDefault();
window.open(url, 'authWindow', "width=800,height=436");
});

If the page in the popup is in the same domain as yours, you can use window.opener to access data of the opener windows
In your current window :
var winopened;
$("#popup").click(function(e) {
var url = $(this).attr('href');
e.preventDefault();
winopened = window.open(url, 'authWindow', "width=800,height=436");
});
if(winopened) {
// call a function in the opened window :
res = winopened.functionInWindow();
}
hope it helped ...

window.open will return your window object.
var myWindow = window.open
Now you can play with myWindow.

The other posters answers are correct, in that they'll give you the window object. I'm not sure that's what you were asking for.
Here's a very simple way you can return the HTML from the page, if that's the "data" you need (otherwise disregard this answer).
This uses jQuery's AJAX $.get function to load HTML from a webpage, more at https://api.jquery.com/jQuery.get/
$.get(url, function( data ) {
alert( "Data Loaded: " + data );
});
From here, you can do whatever you like with the "data" variable that is returned - store it, parse it, show it...

Related

How can I trigger this function from the URL?

I have this Javascript on my homepage which opens a popup.
I need a way to create a URL which then opens the popup immediately.
I've read on other posts that I need to use parameters and then add something like ?parameter=true to the url. The problem is I don't know javascript.
Can anyone help me with adding parameters to my js code?
jQuery(document).ready(function() {
$("#customButton").click(function () { $("#crazyrocket-launch-icon").trigger("click") });
});
You can use searchParams.Get() to grab the value of the parameter from the URL. You can then run your function on document ready after checking with an if statement.
var url_string = "http://www.example.com/t.html?popup=true";
var url = new URL(url_string);
var popup = url.searchParams.get("popup");
jQuery(document).ready(function() {
if (popup) {
$("#crazyrocket-launch-icon").trigger("click");
}
});

Execute a function after changing location.href

I want to call a function after redirecting the page.
Inside my function I put a parameter which is an ID but when I check the console it wont display. I know I can be able to run it via on click event but I wont get the ID param from the previous page.
Is there a way to get it done?
Code:
function encode(item_id){
$('.js-encode').click(function(){
var url = $(this).data('url');
location.href = url; // new url
save(item_id); // call function after new url finish loading
});
}
function save(item_id){
console.log(item_id); // check if there's item_id exists
}
I agree with Katana's comment. Anything after your redirect statement will not run because the page itself is redirecting. One way that I would suggest to still get the item_id and get around that barrier, would be to include the item_id in the redirect url as a parameter. Then once on the new page, parse that parameter out of the url and save the item_id.
A great example from Cory Laviska's article on Parsing URLs in Javascript, shows how you can get the individual parameters from a URL.
Building onto Manish' answer:
Function on the current page:
function encode(item_id){
$('.js-encode').click(function(){
var url = $(this).data('url');
location.href = url+'?saved_item_id='+item_id; // new url
});
}
Function on the REDIRECTED Page (assuming you only have one parameter)
$( document ).ready(function() {
var url = $(this).data('url');
var item_id = url.queryKey['saved_item_id'];
save(item_id);
});
It may need a few tweeks because I didn't test the code, but hopefully it'll get you on the right track. :)
Hopefully this helps. If it helps and/or answers your question, please select as answer and up vote! :D Feel free to let me know if you have any questions
Try this one
The container is the section of your page where you perform an action.
function encode(item_id){
$('.js-encode').click(function(){
var url = $(this).data('url');
$("#container").load(url,function(){
// other stuffs and functionalities
save(item_id); // call function after new url finish loading
});
});
}
You can try something like this.
function encode(item_id){
$('.js-encode').click(function(){
var url = $(this).data('url');
location.href = url+'?saved_item_id='+item_id; // new url
//save(item_id); // call function after new url finish loading
});
}
/*(function save(item_id){
console.log(item_id); // check if there's item_id exists
}*/
Further more , On New redirected URL you can get item_id which was appended to URL in previous page.
Hope this may help.

Javascript window.open not working

Ok. I'm trying to login to twitter. The window is not opening in this code. The response that gets alerted is not null and is a link to a login screen. Any ideas?
var url = "./twitter_login.php";
var con = createPHPRequest();
con.open("POST",url,true);
con.setRequestHeader("Content-type","application/x-www-form-urlencoded");
con.send("");
var response = "";
con.onreadystatechange = function() {
if(con.readyState==4 && con.status==200) {
response = con.responseText;
alert(response);
window.open(response,"twitter","menubar=1,resizable=1,width=350,height=500");
}
}
The standard popup-blocker logic contained in most browsers these days will block any calls to window.open() that are not the direct result of a user action. Code that is triggered by timers or by any asynchronous callback (like your ajax ready function) will be treated as NOT caused directly by user actions and the new popup window will generally be blocked.
You can verify this is what is happening by temporarily changing your browser's popup blocking (turning it off) and see that it then starts working.
Probably what you need to do as a work-around is to create the window upon the user action that started this thread of code and then put the content into the window when you get your ajax response. The browser will probably allow that. I know that's less desirable from a visual perspective, but you can put some temporary content in the window until the ajax response comes in (something like "loading...").
Just had this exact same issue. Just in case you wanted the code that fixed it. I used this:
newWindow = window.open("", "_blank");
request = $.ajax({ ... my request which returns a url to load ... })
request.done((function(_this) {
return function(data, textStatus, jqXHR) {
return newWindow.location = data.Url;
};
})(this));

Use anchors/hashbangs in the URL to trigger functions on load?

Say someone is linked to the url, example.com/#boom
Can the hashbang in the url, #boom be linked to triggering a function like boom() on the page?
Thanks!
I think you want to do something like this:
window.onload = function(){
var hash = location.hash.substr(1);
if(typeof window[hash] == "function")
window[hash]();
};
If the function specified in the hash exists, then it will be called on page load.
Not sure to understand what you really want... On your web page, you can add code that runs at page load, that examines the URL and that calls a suitable function accordingly. You will want to use window.location for this.
Easily done with JavaScript:
if( window.location.hash ){
var currentHash = window.location.hash;
alert( "Yup! " + currentHash + " is set." );
}else{
alert( "Nope!" );
}
Now this is an example. Obviously you would want to call your call back function instead of outputting currentHash.
For more information on calling the function: http://w3future.com/html/stories/callbacks.xml
Maybe something like this:
window.onload = function(){ // Page onload
if(location.hash == "#boom"){ // Hash is "boom"
boom(); // call function boom()
}
}

window.open and searchpopop

We are use window.open for open popup. But then we want find it and close. Unfortunately we can`t save this popup handle to variable.
P.S. How get list of all windows?
This should work:
var wh = window.open(..)
wh is the handle to the popup window.
If you have control over the page that loads the script, you could do something like this. Warning: this is a really scary and generally bad thing to do:
<script>
var windowHandles = {};
(function() {
var realOpen = window.open;
window.open = function(url, name, features) {
windowHandles[name] = realOpen(url, name, features);
};
})();
</script>
That will build an object (windowHandles) in which the handles for each opened window will be saved.
Put that script in your page before the script that opens the other window is loaded.
I found not perfect solution, but it work.
win = window.open(null, 'Window1');
This code search search window with this name and return handler, but if window is closed it open empty popup.
I Think this is temporary solution
I don't like this solution. Fixing the script to give you a handle would be a better bet.
<button onclick="go()">Go</button>
<button onclick="stop()">Stop</button>
<script type="text/javascript">
function go() {
// Existing function. It opens a window with a name.
window.open('http://google.com', 'test', 'width=300,height=300');
}
var foo;
function stop() {
// Open a new window with the same name. It replaces the existing window.
// Since it opens a local document, the Same Origin Policy does not apply.
// ... and we can capture its return value to grab a handle on an existing
// window
foo = window.open('black-local-page.html', 'test', 'width=300,height=300');
// Give the local page time to load
setTimeout(continue_stopping, 500);
}
function continue_stopping() {
// Call window.open() on the window
foo.close();
}
</script>

Categories

Resources