By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Populate div with website using URL

P: 4
Im new to the whole javascript game, I know pretty much just enough to be dangerous with no real substance. Anyway Im trying to populate a DIV with a website using the URL as an inner html source


Here is my code I keep getting an error object expecte don line 58. Which I will highlight. Anyway Not only would i Like to know what that error is about but also if my logic is correct or if I can even do what im trying... and if not what other solutions are there besides iframes.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <script langauge="text/javascript">
  5.  
  6.  
  7. function reportsnagger(str)
  8. xmlHttp=GetXmlHttpObject();
  9. if (xmlHttp==null)
  10.   {
  11.   alert ("Your browser does not support AJAX!");
  12.   return;
  13.   } 
  14. var url="";
  15. url=url++str;
  16. xmlHttp.onreadystatechange=stateChanged;
  17. }
  18.  
  19. function stateChanged(showcustomer.url) 
  20. if (xmlHttp.readyState==4)
  21. document.getElementById(reportspace).innerhtml=url
  22.  
  23.  
  24. }
  25.  
  26. function GetXmlHttpObject()
  27. {
  28. var xmlHttp=null;
  29. try
  30.   {
  31.   // Firefox, Opera 8.0+, Safari
  32.   xmlHttp=new XMLHttpRequest();
  33.   }
  34. catch (e)
  35.   {
  36.   // Internet Explorer
  37.   try
  38.     {
  39.     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  40.     }
  41.   catch (e)
  42.     {
  43.     xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  44.     }
  45.   }
  46. return xmlHttp;
  47. }
  48.  
  49. </script>
  50.  
  51. </head>
  52. <body>
  53. <form> 
  54. Select A Report:
  55. <select name="customers" onChange="reportsnagger(this.value)"> //this is line 58
  56. <option value="">Select an option please
  57. <option value="URL EDITED">choice1
  58. <option value="NORTS ">choice2
  59. <option value="WOLZA">choice3
  60. </select>
  61. </form><p>
  62. <div id="reportspace"><b>Report info will be listed here.</b></div>
  63. </p></body>
  64. </html>
Jul 17 '08 #1
Share this Question
Share on Google+
6 Replies


acoder
Expert Mod 15k+
P: 16,027
First of all, welcome to Bytes!

There's a number of mistakes you've made. I would suggest you look at the source of a working example, or a simple Ajax tutorial to get you started. Check out the links in this thread.

Just a question though: is the URL from a different domain, i.e. not your site?

PS. changed the title to give the thread a better description.
Jul 17 '08 #2

P: 4
aside from my mistakes if the site is on my web server i should be able to use as URL as innerhtml?

I have the basics down I believe, I understand there were some mistakes in my code could you be a dear and point them out? I know Im asking a lot especially for a new user but none of the references I've turned to have been much help asking specific question to a real life person would help me out tremendously.

If I can't use the object.innerhtml function then what should I use instead? I have the concept of what I want to do down, and have a basic understanding of how the script will be set up to handle it as you can see... I just don't have the practical part. All the tutorials I've read and the O'Reilly book I purchased cover some very usefull things but never specifically cover this issue as far as i can see.

While were on the subject will i need to call all the child and parent (nodes?) to make the site I pull in work correctly or will just simply referencing the URL allow it to function. Im sorry for being so needy but I really am at my wits end and none of the google grease i've been using has been helping.

BTW thanks for the welcome and Im glad your willing to help.
Jul 18 '08 #3

acoder
Expert Mod 15k+
P: 16,027
If the website is from a different domain, i.e. not from your own site, you cannot access it unless you use some server-side code.

As for the mistakes, see these quick changes:
Expand|Select|Wrap|Line Numbers
  1. url = // a valid url
  2. url=url+str;
  3. xmlHttp.onreadystatechange=stateChanged;
  4. }
  5.  
  6. function stateChanged() 
  7.     if (xmlHttp.readyState==4)
  8.     { 
  9.         if(xmlHttp.status == 200) 
  10.         {
  11.             document.getElementById("reportspace").innerHTML=xmlHttp.responseText;
  12.         }
  13.  
  14.     }
  15. }
  16.  
Jul 19 '08 #4

P: 4
The site I am trying to access is on my domain. I just wondered if I can use the document.innerhtml function or do i need another function to pull the page? Because it's technically html but i don't know if it will pull in the code or the precompiled page.

Also thanks for the code help, it's easy to over look those kind of errors in your own stuff.
Jul 22 '08 #5

P: 4
i've rewritten my javascript but i need some help... the reference I used gave generic examples and Im not sure how to apply them to my specific situation like what variables to use... I hate coming here and essentially asking you to do this for me but I really am helpless...

heres the new code..

Expand|Select|Wrap|Line Numbers
  1. var request = false;
  2. var response;
  3. var currentObj;
  4.  
  5. function clearData(reportspace){
  6.     document.getElementById(reportspace).innerHTML = '';
  7. }
  8.  
  9. function loading(reportspace){
  10.     document.getElementById(reportspace).innerHTML = "Loading...";
  11. }
  12.  
  13. function ajaxRequest(url, data, reportspace){
  14.     currentObj = obj;
  15.     loading(currentObj);
  16.     if (window.XMLHttpRequest){
  17.         try{
  18.             request = new XMLHttpRequest();
  19.         }catch(e){
  20.             request = false;
  21.         }
  22.     }else if (window.activeXObject){
  23.         try{
  24.             request = new ActiveXObject("Msxml2.XMLHTTP");
  25.         }catch(e){
  26.             try{
  27.                 request = new ActiveXObject("Microsoft.XMLHTTP");
  28.             }catch(e){
  29.                 request = false;
  30.             }
  31.         }
  32.     }
  33.     request.onreadystatechange = handleData;
  34.     request.open("POST", url, true);
  35.     request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  36.     request.send(data);
  37.  
  38. }
  39.  
  40. function handleData(){
  41.     if (request.readyState == 4){
  42.         if (request.status == 200){
  43.             populateData(request);
  44.         }
  45.     }
  46. }
  47. function populateData(request){
  48.     data = request.responseText;
  49.     if (data != null){
  50.         clearData(currentObj);
  51.         document.getElementById(currentObj).innerHTML = data;
  52.     }else{
  53.         document.getElementById(currentObj).innerHTML = '';
  54.     }
  55. }
  56.  
the html page is the same except im now pulling the javascript in from a .js instead of imbedding it. Im still getting the object expected error on line 10 of the html any help would be appreciated
Jul 24 '08 #6

acoder
Expert Mod 15k+
P: 16,027
You've not defined obj (line 14 in previous post) anywhere.
Jul 29 '08 #7

Post your reply

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