dynamically loaded DIV

C

cmt

I have a <div> on my page that is dynamically loaded via javascript.
This part works fine, however any javascripts or javascript function
calls that happen to be in the dynamically loaded content, do not
work!

Not even a simple alert('hi!'); works...it's like the browser ignores
any scripts that are in the dynamically loaded <div>.

Is there a way around this?

Thanks!
 
S

SAM

cmt a écrit :
I have a <div> on my page that is dynamically loaded via javascript.
This part works fine, however any javascripts or javascript function
calls that happen to be in the dynamically loaded content, do not
work!

Which borwser ?
(all do not react similar)
Not even a simple alert('hi!'); works...it's like the browser ignores
any scripts that are in the dynamically loaded <div>.

I think it can read CSS (In tests that I have done with Firefox)
Is there a way around this?

Have a look here to see how and when a browser understands there could
be a new script :
<http://members.aol.com/_ht_a/hikksnotathome/loadJSFile/>

Probably you'll have to
- create a new element 'script'
- innerHTML in this new element the script element (its content ?)
just loaded in the div, or just before to display the responseText
- then insert this new script in the body or the header
 
C

cmt

I have written javascript calls inside a dynamically written <div> in a
return from AJAX (which is then handled by javascript). Those calls
worked. Can you show your code?

Here is the code that I use to load the page into the div tag:

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 ||
window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added
to page
}

Thanks
 
S

SAM

cmt a écrit :
Here is the code that I use to load the page into the div tag:

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 ||
window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

Where are the tags 'script' ?

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 ||
window.location.href.indexOf("http")==-1))
var S = document.createElement('SCRIPT');
S.type = "text/javascript";
S.innerHTML = page_request.responseText;
document.getElementById(containerid).appendChild(S);
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added
to page
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 ||
window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText;
if((fileref!=""){
var S = document.createElement('SCRIPT');
S.type = "text/javascript";
S.innerHTML = page_request.responseText;
document.getElementsByTagName("head").item(0).appendChild(S);
loadedobjects+=file+" " //Remember this object as being already added
to page
}
}
 
P

pr

cmt said:
Here is the code that I use to load the page into the div tag:

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 ||
window.location.href.indexOf("http")==-1))

The implication of that is that you are prepared to ignore the HTTP
status of a response to your request provided that the current document
isn't served using an http or https protocol. If that's what you mean,
there are better ways to express it, and it makes me wonder what
environment you're using (file:// urls perchance?), whether you have yet
succeeded in displaying downloaded non-script content in your div and
whether you're absolutely certain there is no error message in whatever
browser you are using. More detail please.
document.getElementById(containerid).innerHTML=page_request.responseText
}

if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added
to page
}

We cannot judge that without knowing what 'fileref' is. Or
'loadedobjects'. Or 'file'.
 
C

cmt

The implication of that is that you are prepared to ignore the HTTP
status of a response to your request provided that the current document
isn't served using an http or https protocol. If that's what you mean,
there are better ways to express it, and it makes me wonder what
environment you're using (file:// urls perchance?), whether you have yet
succeeded in displaying downloaded non-script content in your div and
whether you're absolutely certain there is no error message in whatever
browser you are using. More detail please.

We cannot judge that without knowing what 'fileref' is. Or
'loadedobjects'. Or 'file'.

I apologize, I mistakenly only posted a portion of the function. Here
is the whole javascript function:

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() :
"?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 ||
window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object
has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added
to page
}
}
}

Thank you!
 
P

pr

cmt said:
....?


I apologize, I mistakenly only posted a portion of the function. Here
is the whole javascript function:

Nice try, but best of all would be a URL people could try out. I don't
see any obvious mistakes here, so it could be a problem with what you're
downloading, the HTML into which you're inserting it, or simply a part
of the script we haven't seen yet.

In any case, nothing that can be identified easily without a working
example.
function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page

This variable is undefined.
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() :
"?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 ||
window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){

This function is never called.
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object
has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added
to page
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top