Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

input/output stream

 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to read the content of a url, do some changes and again publish it. I have used the following code just to test how to read and write streams but it doesn't work.

<html>
<head>
<script language="JavaScript">
function readfile (){
inputstream = readfile("http://www.somesite.com");
file_put_contents("php://output", inputstream);
}
</script>
</head>

<body onload="readfile ()">
</body>
</html>

any suggestion?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you find documentation for a readfile() function in JavaScript?
 
nastaran zanjnai
Ranch Hand
Posts: 43
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
php is not JavaScript.
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry. an stupid mistake. is there a way in javascript to do that?
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please let me know if there is a way in javascript to read the content of a webpage or not?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Impatient, aren't we? Was it ok that I got some sleep?

Ajax.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to use the XMLHttpRequest Object. Only problem is that
JavaScript has a Same Origin Policy so it can not read fro other domains. If the file is in a different domain, than you will have to use a serverside proxy.

Eric
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. I tried to work with XMLHttpRequest
but for example the following code doesn't run



[ Bear: added code tags ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are following an Ajax tutorial, you need to look at it more carefully. You are checking for the status before the request completes asynchronously.

Then, once you understand the basics, adopt a library such as jQuery or Dojo to do your Ajax for you. Few senior developers will do Ajax "by hand".
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what I wrote is exactly what is explained in webpage to do a Synchronous request. I've also tried the Asynchronous request suggested in the above page but nothing changed.


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I missed the synchronous part. Well, "nothing changed" doesn't tell us much. What is happening? Is any HTTP request being sent? If so, is the response correct? What other debugging steps have you taken?
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem was that I am using IE6 and should create the new XMLHttpRequest object as


thanks for your help
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, this is one small example of why doing Ajax "by hand" is inadvisable. There are too many nuances and browser differences to re-invent the wheel when someone else has already solved all the problems.
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new in this area, do you mean that there is a more straight forward way to do what I want to do?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are making a call to another domain, JavaScript has the same origin policy so you can not access data from other domains.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of this, it'd best to do this on the server in Java or other server-side code.
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I could use the XMLHttpRequest to read the content of the webpage and write it in an activexobject. this is what I have now:


my problem now is to find all <a href...</a> in "txt" and add a check box before them. can you please help me?
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed the code to the following but it just print "object"


can you please help me?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use proper indentation when posting code. I will not read unindented code.
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the page being served from http://www.webpage.com?
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the actual link is "http://www.google.com/search?hl=enrlz=1I7SKPB_en&q=query&sa=N"
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read this?

Eric Pascarello wrote:You are making a call to another domain, JavaScript has the same origin policy so you can not access data from other domains.

Eric
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I read that but as I said before, I can read the webpage. document.write(txt=req.responseText) at line 19 prints the content of the webpage. my question now is about document.write(xmlDoc.getElementsByTagName("a")) at line 35 which just print "object".
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use document.write() after the page is loaded. It replaces the page with a new one.
 
nastaran zanjnai
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is not my problem at the moment. now I need to find all <a href in the webpage.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic