0

Got a little ajax happening using XMLhttpRequest, it replaces a div and works as intended.

But i cant seem to get the included ajax page to run javascript. How can i run javascript from the included ajax page?

ie: mainpage.php has select form, on select change event includepage.php gets loaded into a div on the page and changes based on what is selected in select box, this works fine and as intended using xmlhttp, the issue im having is trying to run javascript from the included page, ie: a simple alertbox.

There is no code to really past, and maybe this is a standard behaviour but i acnt find any infomation on it so any help on how to run javascript from within a included ajax page that replaces a div would be much appreciated.

4
  • Duplicate of: stackoverflow.com/questions/4971254/… Commented Mar 10, 2014 at 4:09
  • Since you found your own duplicate with an answer, you can either delete this question or you can mark yours as a duplicate of that other one. Click on "close" and it will give you a choice for duplicate. Commented Mar 10, 2014 at 4:14
  • I cant really make sense of that answer so feel free to contribute here.... Commented Mar 10, 2014 at 4:17
  • That is the problem with duplicate questions. Sometimes their answers aren't always that great. I've added an answer that explains how it works. Commented Mar 10, 2014 at 4:35

2 Answers 2

1

When you insert scripts in content via innerHTML, those scripts don't get run by the browser.

If you want them to run, then you need to find the scripts in that content, get the text from them and insert them into new scripts that are then inserted into the document. As long as the scripts don't need to run in place using document.write(), this will work just fine and libraries like jQuery do this for you automatically when inserting HTML into the document.

Here's a basic demo for how it works: http://jsfiddle.net/jfriend00/2tRQW/

// insert dynamic content that has a script in it
// simulate what's happening with your ajax call
var s = 'Hello<s' + 'cript>alert("Hello");  document.getElementById("result").style.color = "red";</s' + 'cript>';
var obj = document.getElementById("result");
obj.innerHTML = s;

// find the script tags in that dynamic content
var scripts = obj.getElementsByTagName("script");

// create new script tags that you can copy the script content over to
// and then insert the new script tags into the document to make the browser
// actually run the scripts
var head = document.getElementsByTagName("head")[0];
for (var i = 0; i < scripts.length; i++) {
    var oldScript = scripts[i].parentNode.removeChild(scripts[i]);
    var newScript = document.createElement("script");
    newScript.type = "text/javascript";
    newScript.textContent = oldScript.textContent;
    head.appendChild(newScript);
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks guys, i understand now what is going on, now its a easy fix. so essentially we are scraping out all the script tags and then putting them back and that was we get the dynamic php varibles and such in the script required being the main reasion i have the script in includedpage.php.
i think i might just rewrite it all to use jquery would be best option ? Im using mix of jquery and javascript and have code all over the place, back to basics i think, thanks kindly for your assistance.
If you have jQuery already, then by all means use it for this. jQuery .html() or any jQuery construction of objects from an HTML string will already do this for you automatically for any embedded scripts.
1

You don't seems to have any idea about the code right?

So i am giving you one example about this one

one page page1.aspx

Have a javascript function call abc()

Now you can add this function into main page and add uc (usercontrol)

to and call this function like this

var aa=abc();

So abc will be called

If you have two pages separately then also you can call this function because when JavaScript rendered into browser its gets conceited based on files you are including on your page

4 Comments

so i assumed, but its not working as intended, sorry i didnt make that clear, i cant even call js functions that where on mainpage.php in includedpage.php, but i think you are saying to do this one must use a usercontrol ? So i cant put my javascript in includedpage.php it must be in a function in mainpage.php and called from includedpage.php with usercontrol?
No that's not necessary but you can do that easy by usercontrol and its also possible using the javascript file including in a both pages
this below url makes sense to me: stackoverflow.com/questions/4971254/… I cant make sense of what you mean sorry.
this will not work for me for the javascript is dynamic on the include.php page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.