wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes input/output stream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "input/output stream" Watch "input/output stream" New topic
Author

input/output stream

nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

Where did you find documentation for a readfile() function in JavaScript?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
http://php.net/manual/en/wrappers.php.php
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61773
    
  67

php is not JavaScript.
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
sorry. an stupid mistake. is there a way in javascript to do that?
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

Impatient, aren't we? Was it ok that I got some sleep?

Ajax.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Apr 15, 2011
Posts: 43
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

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

Joined: Apr 15, 2011
Posts: 43
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

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

Joined: Apr 15, 2011
Posts: 43
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

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

Joined: Apr 15, 2011
Posts: 43
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

Because of this, it'd best to do this on the server in Java or other server-side code.
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
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

Joined: Apr 15, 2011
Posts: 43
I have changed the code to the following but it just print "object"


can you please help me?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61773
    
  67

Please use proper indentation when posting code. I will not read unindented code.
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61773
    
  67

Is the page being served from http://www.webpage.com?
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
the actual link is "http://www.google.com/search?hl=enrlz=1I7SKPB_en&q=query&sa=N"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61773
    
  67

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

Joined: Apr 15, 2011
Posts: 43
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

You cannot use document.write() after the page is loaded. It replaces the page with a new one.
nastaran zanjnai
Ranch Hand

Joined: Apr 15, 2011
Posts: 43
it is not my problem at the moment. now I need to find all <a href in the webpage.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: input/output stream