Jump to content

Issues with onload

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Injury

Injury

    Newbie

  • Members
  • PipPip
  • 19 posts
Hello again! I've gone back and started from scratch! I need your help again codecall!

I want to dynamically load txt via XMLHttpRequest, but I can't seem to get the code to work with an onload event handler. please! help me gurus!

here is some code!
html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Plaid Apple Studios</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link href="/css/main.css" rel="stylesheet" type="text/css" />

<script src="/js/main.js" language="javascript" type="text/javascript">

loadXMLDoc();

</script>

</head>

<body class="background">

<!-- Not Content -->

<!-- Content -->

<div id="main" align="center"><h1>Plaid Apple Studios</h1></div>

<div id="nav">Home</div>

<div id="content"></div>

</body>

</html>

javascript

//main load script

function loadXMLDoc()

{

alert("Sent");

if (window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.onreadystatechange=function()

  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

    document.getElementById("content").innerHTML=xmlhttp.responseText;

    }

  }

xmlhttp.open("GET","/txt/content.txt",true);

xmlhttp.send();

}



Edited by Injury, 10 February 2011 - 10:04 PM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I haven't tested it, but it seems this would generate an invalid function:
function loadXMLDoc()
alert("Sent");
{
Maybe try putting the alert inside the function
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Injury

Injury

    Newbie

  • Members
  • PipPip
  • 19 posts
I noticed that and changed it after posting. Whoops! Small mistakes aside...

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Ah, it is possible that you are not allowed to put script if you are including it in the same tag, this seemed to work for me:
<script src="/js/main.js" language="javascript" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
loadXMLDoc();
</script>

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Injury

Injury

    Newbie

  • Members
  • PipPip
  • 19 posts
I got it! Thanks again! :)

EDIT:

How would I send the function a value, such as a number, or a name of a text file to call sequential .txt files? I was trying some stuff out, but it doesn't seem to work for me!
UGH...

Edited by Injury, 12 February 2011 - 01:54 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users