Welcome to WebmasterWorld Guest from 100.26.182.28

Forum Moderators: open

Message Too Old, No Replies

http request in javascript

http request in javascript

     
7:54 pm on Jun 21, 2009 (gmt 0)

New User

10+ Year Member

joined:June 21, 2009
posts: 2
votes: 0


Hello, i am trying to make a single div tag reload at a given time with contents from an external file.
i got it to work with ajax but that method only transfers the static html of the external file, leaving out other dynamical javascripts and probably php too.
Can someone please tell what is wrong with this code?
I've been working on it all day and can't seem to figure it out.
The reason for this method is that i do want to trigger the browsers reloading animations.

File1:
<html>
<body>
<script>
function reload(){
var oRequest = new XMLHttpRequest();
var sURL = "http://"+self.location.hostname+"/media/2.php";

oRequest.open("GET",sURL,false);
oRequest.setRequestHeader("User-Agent",navigator.userAgent);
oRequest.send(null)
document.getElementById("new").innerHTML = oRequest.responseText;
setTimeout("reload()",4000);
}
window.onload = reload;
</script>
<div id="new"></div>
</body>
</html>

File2 2.php - the one included
<div><script>
var rand_no = Math.random();
document.write(rand_no);
</script>
file2</div>

This test version of the code should retrieve a obviously inside the <div id="new"></div> a random number followed by the text "file2", but only retrieves the text file2.
In other words all that is dynamically generated in the inclusion file is being stripped off.

Any ideas?

1:54 am on June 22, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


innerHTML excludes script. Can the script stuff be done in PHP ?
4:59 pm on June 22, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 25, 2004
posts:161
votes: 0


im having the same problem..

im loading an html file into a div
(and also fading out old content, and in with new content)
and it works fine until the html file i load into the div trys to access dynamic data from an xml file, in which case all i get are the variable names displayed.

i understand why the javascript isnt being processed, but i thought that the spry framework would have a solution for this....

8:24 pm on June 22, 2009 (gmt 0)

New User

10+ Year Member

joined:June 21, 2009
posts: 2
votes: 0


i have tried with javascript, ajax+php, and flex and can't make it load, or can't make it load without triggering the browsers animation which was my main scope.
I have a game site and inside the <div> i want to change i have among others js scripts like google ads and custom js that displays related games, banners etc.
i was trying to make that div dynamical so i could change it's content every few seconds without triggering browser animations.
can not use frames because i would be breaking the TOS of adsense and on top of that they are bad for seo.

i posted here the php+ajax method <snip>

first of all javascript can be processed, but you have to break it down:
<script>
var divjs = "var rand_no = Math.random();
document.write(rand_no);"

divTag.innerHTML ="<script>"+ divjs+"<\/script>";
</script>
i used this on another website and it works.
the problem with processing javascript from javascript is that end tag </script> it closes the script to early. You have to either escape the slash <\/script> or break it: "</sc"+ript>";

to:delboy1978uk
for your problem i suggest pre-loading the stuff you want shown(if it's not to big) and put in another hidden div:

<div id="hidden" style="display:none;> data from xml</div>
<div id="visible"> old content</div>
<script>
function swap()
{
document.getElementbyID.("hidden").style.display.block;
document.getElementbyID.("visible").style.display.none;
}
setTimeout("swap()",5000)
</script>
not sure about the syntax.
That is if you only want to change the content once.

to daveVk

how do you suggest doing it in php?

[edited by: engine at 7:57 am (utc) on June 23, 2009]
[edit reason] No urls, thanks, see WebmasterWorld TOS [/edit]

12:55 am on June 23, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


For your example, making sure reply not cached

<div>
<?php echo rand() ?>
file2</div>

If your real case can be done in php is another matter.

Another option may be to return json from ajax request.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members