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

how to follow a javascript link programaticaly?

P: n/a
ido

Hi all this is my first post here :)

I would like to know something about using javascript programaticaly
The thing is i have this web site on which when i press a button i
uses a function of javascript.. says exactly this in source

<a href="javascript:np('1')" ...

now when i am on that page and i type in my browsers comand line (o
address field)
javascript:np('1')
it works just as if i would have pressed the button...

now my question is how can i do the same programaticaly... i trie
using webrequest

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(textBox1.Text);
req.Method = "Post";
string strPost = "javascript:np('1')";
req.CookieContainer = new CookieContainer();
req.ContentType ="application/x-www-form-urlencoded";
Encoding utf8 = new UTF8Encoding();
byte[] content = utf8.GetBytes(strPost);
req.ContentLength = content.Length;
using (Stream requestStream = req.GetRequestStream())
{
requestStream.Write(content, 0, content.Length);
}
HttpWebResponse Response = (HttpWebResponse)req.GetResponse();
StreamReader sr = ne
StreamReader(Response.GetResponseStream(),Encoding .GetEncoding(1252));
string t = sr.ReadToEnd();
sr.Close();
Response.Close();
richTextBox1.Text=t;

like this just to dump the source of the destination page into
textwindow but when i do
req.GetResponse();
i get an exception:
The remote server returned an error: (501) Not Implemented.

is there something im doing wrong and is there a way to do this?
I mean sure there is coz the browser does this but im just weak wit
javascript and new to web
programming.. so im asking for a little help :)

Thank you for your time
Id

--
idoPosted from http://www.pcreview.co.uk/ newsgroup acces

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

The browser - surprise, surprise! - does not perform a roundtrip to the
server at all when the "javascript:" protocol is used. For example, type the
following in the browser's address line:

javascript:alert('Hello ido!'); and hit Enter.

So, to run page's JavaScript code from your C# code, you will need to do the
same thing the browser does - to parse the HTML, to extract all pieces of
JavaScript, load them all to the scripting engine (you can use the MS Script
Control OCX through COM interop as the scripting engine), expose the
document object model to the script code, to wire up events etc. ...

Sounds scary? Well, you can possibly try to have the Web Browser control to
load the page and then use the Navigate method with the "javascript:"
protocol link. This approach has its own drawbacks, but is definitely easier
than re-implementing a half of the browser. Probably there is some more
deep-level automation of the Internet Explorer - I saw people mentioning
some HTMLDocument object they tried to use. Hopefully someone who has
experience automating IE might reply here - you you should re-post your
question in an IE automation newsgroup and then return here with a follow-up
question on how the suggested thing can be done in .NET.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"ido" <ido.1qsfnm@> wrote in message
news:Iu********************@giganews.com...

Hi all this is my first post here :)

I would like to know something about using javascript programaticaly.
The thing is i have this web site on which when i press a button it
uses a function of javascript.. says exactly this in source

<a href="javascript:np('1')" ...

now when i am on that page and i type in my browsers comand line (or
address field)
javascript:np('1')
it works just as if i would have pressed the button...

now my question is how can i do the same programaticaly... i tried
using webrequest

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(textBox1.Text);
req.Method = "Post";
string strPost = "javascript:np('1')";
req.CookieContainer = new CookieContainer();
req.ContentType ="application/x-www-form-urlencoded";
Encoding utf8 = new UTF8Encoding();
byte[] content = utf8.GetBytes(strPost);
req.ContentLength = content.Length;
using (Stream requestStream = req.GetRequestStream())
{
requestStream.Write(content, 0, content.Length);
}
HttpWebResponse Response = (HttpWebResponse)req.GetResponse();
StreamReader sr = new
StreamReader(Response.GetResponseStream(),Encoding .GetEncoding(1252));
string t = sr.ReadToEnd();
sr.Close();
Response.Close();
richTextBox1.Text=t;

like this just to dump the source of the destination page into a
textwindow but when i do
req.GetResponse();
i get an exception:
The remote server returned an error: (501) Not Implemented.

is there something im doing wrong and is there a way to do this?
I mean sure there is coz the browser does this but im just weak with
javascript and new to web
programming.. so im asking for a little help :)

Thank you for your time
Ido
--
idoPosted from http://www.pcreview.co.uk/ newsgroup access


Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.