0

im trying to make a webapp that uses d-pad navigation so i work with keys on keyboard , for some reason i dont want to reload or redirect the page so i just change the content using ajax :

        var xhr;
        xhr=new XMLHttpRequest();
        xhr.open("GET","pages.php?page="+page ,  false);
        xhr.send();
        $('body').html(xhr.responseText);

ok it works fine and the content is replaces without problem but the javascript in the content is not executing...

NOTE: the reason i dont want to put javascript in the head section of the page is like i sad im working with keys and on diffrent pages i act diffrent with each keypress ...

k961
  • 577
  • 1
  • 5
  • 19
  • what kind of javascript code are you injecting? you should make the calls to the code you added AFTER inserting it – Lorenzo Marcon Feb 13 '14 at 09:49
  • Also why mix jQuery and initiating your own AJAX? If you're already using jQuery, you should use their relevant AJAX features – casraf Feb 13 '14 at 09:49
  • This seems like it shouldn't even be a problem in the first place. Can you elaborate on what your application is? – Blender Feb 13 '14 at 09:51
  • i use js to add an event listner for key presses and then i get the keycode for it ... so that i can do other things with that ... – k961 Feb 13 '14 at 09:51
  • @Blender its a web app for tv – k961 Feb 13 '14 at 10:00

3 Answers3

0

the problem over here is you are including all the content in your php page, with html and javascript code as html content to the body...which means as a text to the body it is appending the data. So your scripts won't work as expected.

TKV
  • 2,533
  • 11
  • 43
  • 56
0

Using the jquery get function:

$.get("pages.php?page="+page,function(data){
    $("body").html(data);
});
Chris Charles
  • 4,406
  • 17
  • 31
0

Just Make a Try,

May be you can make use of eval(), (But as said, eval is evil).

Refer this: Executing javascript script after ajax-loaded a page - doesn't work

Community
  • 1
  • 1
RONE
  • 5,415
  • 9
  • 42
  • 71