-1

Is it possible to make a ajax request on one page with a response on another loaded page? I saw something similar but the page needed to be loaded with the response, but my page is already loaded. Is there a way? I tried something like this:

$('#videoSRC').click(function(e){

        var url = $('#video_url').val();
        $.ajax({
            url:'server.php',
            type:'GET',
            data:{url:url},
            success: function(){

                $('#container').append('success!');
            }

        });

    }); 

But is not working So the button "#videoSRC" is on the first html page and the "#container" div is on another loaded html page. Is it a way to do this ?

alin dradici
  • 201
  • 2
  • 4
  • 11
  • your question doesn't make much sense... only one page is loaded at a time per page, unless you're including iframes, but that's a bit different... – Kevin B Dec 19 '16 at 21:17
  • 1
    Check this out http://stackoverflow.com/questions/4079280/javascript-communication-between-browser-tabs-windows NOT the accept answer from that, but the one using local storage – Anthony C Dec 19 '16 at 21:18
  • Are you sure the other page is _"...already loaded."_ It looks like you're loading the new page -> `window.location.href = "index.html";`... – War10ck Dec 19 '16 at 21:23
  • what I am actually trying to do is to remotely manage the content from a loaded page – alin dradici Dec 19 '16 at 21:25

2 Answers2

0

The answer is NO, because once you have executed:

window.location.href = "index.html";

Nothing else will execute. And even if the line:

$('#container').append('success!');

executes (maybe due to a delay in charging the other page), then it is still in the first page context.

If you want to do it, you will need to maybe save some data in cookie or localStorage, and then check that data when the "index.html" is loaded to execute the line:

$('#container').append('success!');

Something like this: first page

$('#videoSRC').click(function(e){

    var url = $('#video_url').val();
    $.ajax({
        url:'server.php',
        type:'GET',
        data:{url:url},
        success: function(){
            localStorage.success = true;
            window.location.href = "index.html";
        }

    });

}); 

Second page

$(document).ready(function() {
   if(localStorage.success) {
      $('#container').append('success!');
   }
});
B. Assem
  • 1,058
  • 9
  • 12
0

If you need to show other info on an already loaded page you then need to use localstorage

Like

if (typeof(Storage) !== "undefined") {
  //if supports
localStorage.setItem("lastname", "Smith");
} else {
// Sorry! No Web Storage support..
 }

Then on the other page retrieving is as

localStorage.getItem("lastname")
Geoff
  • 6,277
  • 23
  • 87
  • 197