dynamic content does not fire javascript.

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2006
Posts: 48
Reputation: yilmazhuseyin is an unknown quantity at this point 
Solved Threads: 5
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

dynamic content does not fire javascript.

 
0
  #1
Jun 11th, 2009
Hi I am working on a little project I'd like to call post database and I got a little problem. when user clicks a page number, I bring html content from server and add it to page. And I can see the new content on the page. But if new html content has some java script it does not run.
you can see the example on
postdatabase.appspot.com
go to tutorials page, and click page 3 . new page will come. including following alert inside
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. alert('asdf');
But it show alert window. Somehow browser does not fire javascripts.
Here is my guess: it does not work because you can not use javascript to shape pages after page load.(I just made that up. but there is no other logical explanation I can come up with.)
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 4
Reputation: jodin is an unknown quantity at this point 
Solved Threads: 1
jodin jodin is offline Offline
Newbie Poster

Re: dynamic content does not fire javascript.

 
0
  #2
Jun 11th, 2009
Hi, you have to add an Id to the script

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript" id="evalMe">
  2. alert('asdf');
  3. pdbinit(23001);
  4. </script>
and when the dynamic content is load you have to find the script getElementById('evalMe') and apply eval.

:-)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 48
Reputation: yilmazhuseyin is an unknown quantity at this point 
Solved Threads: 5
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

Re: dynamic content does not fire javascript.

 
0
  #3
Jun 12th, 2009
First of all thank you for quick response.

Let me tell you what I am trying to do.in this project. I go to settings page and create a new wall. then in my page (in this case tutorial page 3). than I call pdbinit function. which writes two div elements with document.write() and then fill inside divs with the data it gets from server. now here is the problem. if I use eval ,I wont be able to use document.write because My text will already be converted to elements and added to htmldom. so what I need is to use document.write in the text of innerhtml.

I simplified the question here is the code.

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function inject(targetDiv){
  5. var content = '<b>This is the text before script</b> <script type="text/javascript">document.write("<br>This part is written via script")</script';
  6. content +='>';//I am adding this here to broke </script!> tag
  7. targetDiv.innerHTML = content;}
  8. </script>
  9. </head>
  10. <body>
  11. <div id="myDiv"></div><br>
  12. <input type="button" value="inject HTML" onclick="inject(document.getElementById('myDiv'))"/>
  13. <input type="button" value="Call Document.write after page load" onclick="document.write('Calling document.write after page load')"/>
  14. </body>
  15. </html>

Here I have a button when I click it , I inject an html (including some script) into the page. but script does not run. how can I run this. I also added a button to run document.write. if you press it it will run document.write. which will overwrite the content of the page. this has to be called by browser only when browser convert text to htmldom elements. (I am just guessing right now.)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: dynamic content does not fire javascript.

 
0
  #4
Jun 12th, 2009
Try this one:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. <!--
  5.  
  6. var inject = function( targetDiv ) {
  7. targetDiv.innerHTML = "";
  8.  
  9. var content = "<b>This is the text before script</b>\n";
  10. var script = document.createElement("script");
  11. script.type = "text/javascript";
  12. script.id = "script1";
  13. script.innerText = "function injectFunc() { document.write( '<br>This part is written via dynamic injected script!' ); }";
  14. targetDiv.appendChild( script );
  15. targetDiv.innerHTML += content;
  16. };
  17. //-->
  18. </script>
  19. </head>
  20. <body>
  21. <div id="myDiv"></div><br>
  22. <input type="button" value="inject HTML" onclick="inject(document.getElementById('myDiv'))"/>
  23. <input type="button" value="Call Dynamic Function" onclick="injectFunc();" />
  24. </body>
  25. </html>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 48
Reputation: yilmazhuseyin is an unknown quantity at this point 
Solved Threads: 5
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

Re: dynamic content does not fire javascript.

 
0
  #5
Jun 12th, 2009
actualy that did not work either.I tried it on windows (ff , ie , chrome). It worked as same as before. and there is another problem. innerText of new script element looks empty from firebug. I suspect that innerText property is read only.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC