473,503 Members | 5,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xmlhttprequest and innerhtml executing javascript

9 New Member
I am posting html onto my main page between div tags using xmlhttprequest and innerhtml. The html I am posting has javascript inside which I am executing using the eval() function. However, the problem I face is that the javascript uses innerhtml as well and will not work for some reason. I have tested the javascript and it is definitely executing, it just will not change the innerhtml.

To be more clear, the innerhtml is trying to access the html that I am posting onto my main page. So what is happening so far is:

Get HTML
POST HTML between div tags
eval javascript
and now innerhtml should change

Thanks in advance!
John
Jun 28 '07 #1
15 6979
rage3324
9 New Member
Just bumping the message up

I am posting html onto my main page between div tags using xmlhttprequest and innerhtml. The html I am posting has javascript inside which I am executing using the eval() function. However, the problem I face is that the javascript uses innerhtml as well and will not work for some reason. I have tested the javascript and it is definitely executing, it just will not change the innerhtml.

To be more clear, the innerhtml is trying to access the html that I am posting onto my main page. So what is happening so far is:

Get HTML
POST HTML between div tags
eval javascript
and now innerhtml should change

Thanks in advance!
John
Jun 28 '07 #2
gits
5,390 Recognized Expert Moderator Expert
hi ...

please give an example on what you are doing ...

you are writing text to a div and this text contains js ... right? now you parse that text and eval the js ... right? the evaled js uses the same text where it is in? ... perhaps you simplify your code and post an example here ... so that we may have a closer look on it.

kind regards ...
Jun 29 '07 #3
rage3324
9 New Member
In my main file where the HTML/JS below is being posted beteween DIV TAGS, there is a script to run the JS after the xmlhttprequest. In the JS below there is a line that needs to access the innerHTML of the element "lg1". However, this will not display on the page. Any ideas?

SHOP.HTML
[HTML]<body>
<link rel="stylesheet" type="text/css" href="screen.css" />

<script type="text/javascript">
var s = document.createElement('script');
s.src = 'yahoo.js';
document.body.appendChild(s);
...<execute a few more scripts>
</script>
</body>

<script type="text/javascript">

YAHOO.example.DDApp = function() {
return {
init: function() {
var eul = document.getElementById("lg1");
var eli = document.createElement("li");
// eli.appendChild(document.createTextNode("WSJ.com") );
eli.innerHTML = "<image src='barrons.jpg' />";
eli.id = "test";
eli.setAttribute("class", "sortList");
eli.setAttribute("className", "sortList");
eli.price = 19.99
eul.appendChild(eli);

new YAHOO.example.DDList("test");

new YAHOO.example.DDListBoundary("hidden1");
new YAHOO.example.DDListBoundary("hidden2");
new YAHOO.example.DDListBoundary("hidden3");
}
};
} ();
YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT; //Sets the mode to Intersect which provides more freedom when dragging and dropping on a target
YAHOO.util.Event.addListener(window, "load", YAHOO.example.DDApp.init);
</script>

<div id="update">
<td>
<ul id="lg1" class="listGroup1">Products
...
</ul>
</td>
...
</div>
[/HTML]


hi ...

please give an example on what you are doing ...

you are writing text to a div and this text contains js ... right? now you parse that text and eval the js ... right? the evaled js uses the same text where it is in? ... perhaps you simplify your code and post an example here ... so that we may have a closer look on it.

kind regards ...
Jun 29 '07 #4
rage3324
9 New Member
bump up to the top.....
Jun 29 '07 #5
acoder
16,027 Recognized Expert Moderator MVP
<image> is not a valid tag. It should be <img>
Jun 30 '07 #6
gits
5,390 Recognized Expert Moderator Expert
<image> is not a valid tag. It should be <img>
yep ... that is the fatal typo ... i guess ;)
Jul 1 '07 #7
rage3324
9 New Member
Unfortunately that did not make the pictures load :(

Any other ideas?
Jul 2 '07 #8
rage3324
9 New Member
Update.. so I have found that all code inside return { } does not run...

If I run the page as a stand-alone, everything works fine
Jul 2 '07 #9
gits
5,390 Recognized Expert Moderator Expert
and did you resolve your problem?

kind regards ...
Jul 2 '07 #10
rage3324
9 New Member
Nope. It seems that the code inside return { } will only run "on load". I have no idea if there is a way around that or a method to fake-out the browser. I could easily use an iframe to solve this problem, but I am trying to avoid that.

and did you resolve your problem?

kind regards ...
Jul 2 '07 #11
gits
5,390 Recognized Expert Moderator Expert
of course its due to:

Expand|Select|Wrap|Line Numbers
  1. YAHOO.util.Event.addListener(window, "load", YAHOO.example.DDApp.init);
otherwise you have to call it yourself whenever you want ...

kind regards ...

ps: this is relevant: YAHOO.example.DDApp.init
Jul 2 '07 #12
rage3324
9 New Member
So I see it is only listening at load? How should I change this?

of course its due to:

Expand|Select|Wrap|Line Numbers
  1. YAHOO.util.Event.addListener(window, "load", YAHOO.example.DDApp.init);
otherwise you have to call it yourself whenever you want ...

kind regards ...

ps: this is relevant: YAHOO.example.DDApp.init
Jul 3 '07 #13
gits
5,390 Recognized Expert Moderator Expert
simply call the relevant method explicitly when you need to ...

kind regards ...
Jul 3 '07 #14
rage3324
9 New Member
Thanks! Problem is resolved
Jul 3 '07 #15
sxxxydan
1 New Member
I had a heck of a time getting embedded javascript to work within dynamically generated innerHTML. I got some help from some New Zealanders at blackoutwebdesign.com and did a lot of trial and error on my own. The following code should work for anyone in both IE and FireFox. Haven't had an opportunity to test it on Opera, Safari or anything else. If you find something wrong please let me know!

Expand|Select|Wrap|Line Numbers
  1. function getEmbeddedHTML(){
  2.   var my_page_request = false;
  3.   if (window.XMLHttpRequest) my_page_request = new XMLHttpRequest();
  4.   else if (window.ActiveXObject){
  5.     try {my_page_request = new ActiveXObject("Msxml2.XMLHTTP");}
  6.     catch (e){try{my_page_request = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){}}
  7.   }
  8.   else return false;
  9.   var url = '[YOUR URL]';
  10.   var con = '[YOUR UNIQUE DIV/CONTAINER ID]';
  11.   my_page_request.open('GET', url, true);
  12.   my_page_request.onreadystatechange=function(){loadpage(my_page_request, con);}
  13.   my_page_request.send(null);
  14. }
  15. function loadpage(my_page_request, con){
  16.   if (my_page_request.readyState == 4 && (my_page_request.status==200 || myBindexOf(window.location.href,"http",0)==-1)) {
  17.     var myResponse = my_page_request.responseText;
  18.     var myContainer = document.getElementById(con);
  19.     myContainer.innerHTML=myResponse;
  20.     executeEmbeddedScripts(myContainer,myResponse);
  21.   }
  22. }
  23. function executeEmbeddedScripts(node,theResponse){
  24.   var bSaf = (myBindexOf(navigator.userAgent,'Safari',0) != -1);
  25.   var bOpera = (myBindexOf(navigator.userAgent,'Opera',0) != -1);
  26.   var bMoz = (navigator.appName == 'Netscape');
  27.   if (!node) return;
  28.   var myHead=document.getElementsByTagName('head');
  29.   if ((!bSaf)&&(!bOpera)&&(!bMoz)) node.innerHTML="&nbsp;&nbsp;"+theResponse; // World's most retarded IE fix
  30.   var st = node.getElementsByTagName('SCRIPT');
  31.   var strExec;
  32.   var scripts = st.length;
  33.   var i = 0;
  34.   for(j = 0; j < scripts; j++){
  35.     var scriptsAtStart = st.length;
  36.     if (bSaf) {strExec = st[i].innerHTML; st[i].innerHTML = "";}
  37.     else if (bOpera) {strExec = st[i].text; st[i].text = "";}
  38.     else if (bMoz) {strExec = st[i].textContent; st[i].textContent = "";}
  39.     else {strExec = st[i].text; st[i].text = "";}
  40.     try {
  41.       var myScript = document.createElement("script");
  42.       myHead.item(0).appendChild(myScript);
  43.       myScript.type = "text/javascript";
  44.       myScript.text=strExec;
  45.       var html = node.innerHTML;
  46.       // jscript would prematurely eject if it encountered the full closing tag, even encapsulated in a string object
  47.       var endScriptL = '<' + '/script>';  
  48.       var endScriptU = '<' + '/SCRIPT>';
  49.       var pos2 = myBindexOf(html,strExec,0);
  50.       var pos = myBindexOf(html,endScriptL,pos2) >= 0 ? myBindexOf(html,endScriptL,pos2) : myBindexOf(html,endScriptU,pos2);
  51.       pos += String(endScriptL).length;
  52.       html = html.substr(0,pos) + html.substr(pos);
  53.       node.innerHTML = html;
  54.       if(scriptsAtStart==st.length) i++;
  55.     } catch(e) {
  56.       alert("Script execution error: "+e);
  57.     }
  58.   }
  59. }
  60. function myBindexOf(myObject,mySearch,myPos){
  61.   if (myPos==null) myPos=0;
  62.   if (typeof(myObject)!='object') return -1;
  63.   if(!myObject.indexOf) {
  64.     for(var i=myPos; i < myObject.length; i++) if(myObject[i]==mySearch) return i;
  65.     return -1;
  66.   } else return myObject.indexOf(mySearch,myPos);
  67. }
  68.  
Oh the myBindexOf... yeah I threw that in there cuz we all know how much IE loves indexOf (or lastIndexOf). I know that I could have gone through and replaced all instances of indexOf with something else... but I did it this way. Required a little extra work but I like it.
Jul 5 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

20
4980
by: Gaz | last post by:
In Internet Explorer 6 I'm having a problem with the httprequest object. I use it to call a webservice and display the result in the readystate event handler. This works the first time I call it...
9
2140
by: Phil_Harvey | last post by:
I am redoing my website and trying to get it to do something more exciting using Javascript. I did normal Java at university and code at work in VB.NET. I have got reasonably far into what I want...
6
7246
by: Nathan | last post by:
Can I run two XMLHTTPRequest objects at the same time? Im able to get one to work without problems. If I put a call to a function inside the first ones onreadystatechange function, the 2nd ones...
7
3602
by: pamelafluente | last post by:
The precious input given by Laurent, Martin, Benjamin about XMLHttpRequest in Javascript, has made me think that perhaps I could improve what I am currently doing by using Ajax. Let's make it...
1
7670
by: vocalise | last post by:
The title probably isn't very clear, but I haven't been able to find this problem (or I must be having problems figuring out which search strings to use) so I apologize if this has been addressed...
1
4011
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
20
3996
RMWChaos
by: RMWChaos | last post by:
Currently testing in: WinVista / IE7 I have been working on getting xmlhttprequest going for weeks now. I have finally gotten a semi-working script going. Pulling up text or xml files works great...
7
1760
RMWChaos
by: RMWChaos | last post by:
Bizarro, that's all I can say. Aren't FF2.0.0.8 and NN9 both Mozilla 2 based browsers? So why would the exact same code work in one and not the other? To add insult to injury, it works just fine in...
2
2434
by: gradinafrica | last post by:
I'm trying to create a log out button that uses AJAX to call a php file which ends the current session: //logout.php <?php if (!session_start()); session_destroy(); //Destroys the...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.