0

I'm having major problems. I have a page called dashboard.php but the actual page div (ID = #dashboardcontainer) is empty as I am filling the content with this script:

$('#dashboardcontainer').load('functions/pages/dashboard/dashboard_admin.php');

then I tried:

$.getScript("assets/plugins/jquery-1.8.2/jquery-1.8.2.min.js", function(){
$('#dashboardcontainer').load('functions/pages/dashboard/dashboard_admin.php');
})

However, the page I am wanting to "inject" actually has Javascript in it. The problem is, the above javascript's .load() doesn't load the javascript in the file.

I've tried lots of other examples from the following posts but they don't work at all. The javascript isn't actually loading.

jQuery .load() / .ajax() not executing javascript in returned HTML after appended

$.get('partial.html', function(result){
$result = $(result);

$result.find('#content').appendTo('#new_content');
$result.find('script').appendTo('#new_content');
}, 'html');

= No javascript firing.

jQuery ajax load() java script not executing?

$("loadStuffHere").load("xyz.html"); $.getScript('js/xyz.js');

= Still no javascript firing.

I can't use lots of PHP includes as I receive conflicts which stops my page from rendering correctly.

I've also tried using some sort of Json thing to hide and show dom elements but I hate how complex it is to modify and insert new data.

Is there any simple (this term doesn't seem to exist in the web development dictionary) way I can actually inject a web page that contains it's own javascript in the same way a PHP include would render it?

Here is the PHP page I am trying to inject:

<?php
     echo '<script type="text/javascript">alert("This is the sub-page");</script>';
?>
Community
  • 1
  • 1
  • A Fiddle might help you get a few more responses – Captain John Sep 01 '14 at 21:51
  • I would love to create one but I am still learning all of this kind of stuff. I'd struggle even building a Fiddle that demonstrated my error as my website is a purchased theme from wrap bootstrap (if i am even allowed to mention the site?) – user2355434 Sep 01 '14 at 21:57
  • Is this javascript the only thing you're trying to run? Or do you want to include html too? – flup Sep 01 '14 at 22:16

1 Answers1

0

Try

$.get("functions/pages/dashboard/dashboard_admin.php"
  , function(script, textStatus, jqxhr) {
    console.log(typeof script, textStatus
                , jqxhr.responseText
                , jqxhr.getResponseHeader("Content-Type"));
    $("head").prepend(script);
})
.fail(function(jqxhr, textStatus, errorThrown) {
  console.log(jqxhr.responseText, errorThrown);
});
guest271314
  • 1
  • 15
  • 104
  • 177
  • Hey, thanks for your help! So the page contents is inserted but the actual javascript elements still don't function. I did have to change the $("head") to $("#dashboardcontainer") so it injected the code into the correct area. I left the javascript links in on the parent dashboard.php page, no luck. I moved all the javascripts into the sub-page dashboard_admin.php, still no luck. – user2355434 Sep 01 '14 at 21:57
  • If possible, can post return values of `console.log()` ? Does `jqxhr.responseText` return ` success ` ? – guest271314 Sep 01 '14 at 22:09
  • This is what console.log says: [Log] string success – user2355434 Sep 01 '14 at 22:13
  • Update: I just made a fresh dashboard_customer.php file which echoes the alert() and

    testing

    . This now works. Unusual as the files were identical... However, now I try insert the desired content (panels, graphs and lists) the required javascript references do not work. If I put the required code into dashboard.php the page looks and works great.
    – user2355434 Sep 01 '14 at 22:32