Salam
I have a Problem to load my optional page in AJAX.
I send command to innerHTML
result = http.responseText;
document.getElementById('MyAjax').innerHTML = result;
My result variable contain script tag. But No action is appeard.
Help Me Please.
Thanks at all.
7 11146
To execute JavaScript returned via Ajax, either eval it or add it to the page using dynamic script tags, e.g. - var script = document.createElement("script");
-
script.type = "text/javascript";
-
script.appendChild(document.createTextNode(txt));//txt is the code
-
var head = document.getElementsByTagName("head")[0];
-
head.appendChild(script);
I think I'm having the same issue, and am not sure where to integrate your code snippet into my code to test it.
To clarify my situation, a div is populated with a page, say 'text.html' using innerHTML. Any scripts within text.html do not execute while in the div, but when it is loaded directly, the scripts execute perfectly.
Do you have a larger sample of that code?
if the response contains only javascript, eval the responseText.
if it contains mixed html w/ script tags, IE will not execute the javascript.
you will need to use a regexp to find the code inside the script tag, and eval that after injecting the innerHTML.
if the response contains only javascript, eval the responseText.
if it contains mixed html w/ script tags, IE will not execute the javascript.
you will need to use a regexp to find the code inside the script tag, and eval that after injecting the innerHTML.
Tried that. No change.
Something about my code may be the cause.
This is the code I use to call the inner.HTML -
function xhrRequest(type) {
-
var xhrSend;
-
if (!type) {
-
type = 'text';
-
}
-
if (window.ActiveXObject) {
-
try {
-
xhrSend = new ActiveXObject("Msxml2.XMLHTTP");
-
} catch (e) {
-
try {
-
xhrSend = new ActiveXObject("Microsoft.XMLHTTP");
-
} catch (e) {}
-
}
-
} else if (window.XMLHttpRequest) {
-
xhrSend = new XMLHttpRequest();
-
if (xhrSend.overrideMimeType) {
-
xhrSend.overrideMimeType('text/' + type);
-
}
-
}
-
//document.getElementById('area1').innerHTML += new Date().getTime() + ' Sending xhr:' + xhrSend + '<br />';
-
return (xhrSend);
-
}
-
-
function whatWhere(url, boxid) {
-
var reqType = "text";
-
// alert("WhatWhere");
-
var xhrRec = xhrRequest(reqType);
-
-
-
-
document.getElementById(boxid).innerHTML = "**";
-
xhrRec.open('GET', url, true);
-
xhrRec.onreadystatechange = function() {
-
//document.getElementById('testlog').innerHTML += new Date().getTime() + ' Requesting ' + url + ' ' + xhrRec.readyState + '-' + xhrRec.status + '<br />';
-
if (xhrRec.readyState == 4 && xhrRec.status == 200) {
-
//document.getElementById('testlog').innerHTML += new Date().getTime() + ' Received ' + url + '<br />';
-
document.getElementById(boxid).innerHTML = xhrRec.responseText;
-
//document.getElementById('filename').innerHTML = url;
-
xhrRec = null;
-
} else if (xhrRec.readyState == 4 && xhrRec.status == 404) {
-
document.getElementById(boxid).innerHTML = xhrRec.statusText;
-
//document.getElementById('filename').innerHTML = url;
-
xhrRec = null;
-
}
-
};
-
xhrRec.send(null);
-
}
-
And this is the code on the page that gets called in the div -
<script type="text/javascript">
-
alert("Hello World. The Script Executed");
-
</script>
-
-
-
function xhrRequest(type) {
-
type = type || "html";
-
xhrSend = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
-
if (xhrSend.overrideMimeType) { xhrSend.overrideMimeType("text/" + type); }
-
return xhrSend;
-
}
-
-
-
function whatWhere(url, boxid) {
-
var reqType = "text";
-
var xhrRec = xhrRequest(reqType);
-
document.getElementById(boxid).innerHTML = "**";
-
xhrRec.open("GET", url, true);
-
xhrRec.onreadystatechange = function () {
-
if (xhrRec.readyState == 4 && xhrRec.status == 200) {
-
var rt= xhrRec.responseText;
-
document.getElementById(boxid).innerHTML = rt;
-
var js = tr.replace(/\<script[\w\W]+?\>/i,"").replace(/\<\\script\s*\>/i,"");
-
try { eval(js); } catch(err){ document.getElementById(boxid).innerHTML = String(err); }
-
xhrRec = null;
-
}
-
if (xhrRec.readyState == 4 && xhrRec.status == 404) {
-
document.getElementById(boxid).innerHTML = xhrRec.statusText;
-
xhrRec = null;
-
}
-
}
-
xhrRec.send(null);
-
}
-
-
Thanks for the tips. That's much cleaner.
However, was this a typo rt.replace. Did you intend str.replace or perhaps rt.replace?
I've tried both. str.replace has no effect on the code, and my tweaks didn't have any success. rt.replace throws all kinds of errors. -
var rt= xhrRec.responseText;
-
document.getElementById(boxid).innerHTML = rt;
-
var js = tr.replace(/\<script[\w\W]+?\>/i,"").replace(/\<\\script\s*\>/i,"");
-
-
-
-
var js = rt.replace(/\<script[\w\W]+?\>/i,"").replace(/\<\\script\s*\>/i,"");
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Greg Scharlemann |
last post by:
Given some recent success on a simple form validation (mainly due to
the kind folks in this forum), I've tried to tackle something a bit
more difficult. I'm pulling data down from a database and...
|
by: Alex |
last post by:
Hello.
First, with AJAX I will get a remote web page into a string. Thus, a
string will contain HTML tags and such. I will need to extract text
from one <span> for which I know the ID the inner...
|
by: lcjohnso |
last post by:
Hi all,
Does anyone know if there is an easy way to create the html
representation of an HTMLElement object in javascript? I'm attempting
to update the innerHTML property of a div element to...
|
by: Sandman |
last post by:
Hello,
I'm building a website in PHP and Javascript. The registration
portion is divided into 2 sections:
1. In one, I get info about the visitor. This is sent via POST to a php
script which is...
|
by: paulie |
last post by:
Hi,
I have been experiencing an issue when trying to use AJAX to reload a DIV area
using a timer of 2000ms, which contains a html page with another DIV and javascript.
Scenario
-------------...
|
by: cbellew |
last post by:
Hi guys,
Let me just introduce my problem - i hope someone will be able to help! I have an index page for my site where i want the user to have the choice to view a login form or registration...
|
by: pablosuk78 |
last post by:
Dear coders,
I'm facing a problem and after few research I found that many ppl were looking around for the same solution, hope someone more expert than me could give a good help.
I created an...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
| |