Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I'm upgrading a game i did a while back with php and java script. Its actually Web Broswer based, In the game logic I would ask users questions and give them three threee answers to chhose from, but only one of them will be right. So my page would generate this questions as javascript generated links and place them on the page, and note this links where in a form of images / pictures.

So I'm upgrading the game to a desktop based application for some reasons, I used the Windows IE ActiveX Web Browser control to render webpages from a local server running Apache (not that it matters), The problem is i can get all links in the page that are hard-coded into the page itself. using the HTMLCollection Datastructure, but not the javascript generated one. I tried using an event handler that fires up just after the page is done hoping by then the javascript would have finished generating links and parsed into the page but still no luck.

How can i evaluate JavaScript generated links with my C# code? I just need the logic behind this or some hints. Not the whole code.

Thanks
Posted

Hi mshauny,

OK now I got it. Even though I must admit it's a terrible hack.
But it works:

<html>
	<head>
	</head>
	<body onload="javascript:doIt();">
		<div>Here's a little something</div>
		<div id="test">Nothing to see here!</div>
		<div>Something ...</div>
	</body>
	<script type="text/javascript">
		function doIt()
		{
			alert("Wazzup!?");
			var ele = document.getElementById("test");
			if(ele)
			{
				alert("Right on bro!");
				ele.innerHTML = "<a href='http://www.google.de'>Google schmoogle!</a>";
				window.status="done setting up links"; // this triggers the event in our forms code
			}
			else
			{
				alert("Epic fail, looser!");
			}
		}
	</script>
</html>


void webBrowser1_StatusTextChanged(object sender, System.EventArgs e)
{
    if (webBrowser1.StatusText.Contains("done setting up links"))
    {
        HtmlElementCollection coll = webBrowser1.Document.GetElementsByTagName("a");
        webBrowser1.Document.Window.StatusBarText = "And now for something completely different"; // this keeps our event from retriggering
    }
}


First code block is the html from the former version of my answer. After the links are created dynamically the windows status text is modified. This is detected by the second code block which lives inside the windows forms code file. I couldn't find this event in the Properties/Events dialog ina Visual Studio but I found that such an event exists for webBrowser thanks to MSDN and intellisense. The setting of status text inside event handler to something other than "done setting up links" is nescessary because status text is overwritten for some occurring actions on the page and later restored.
This would cause the if expression to be true more than once, but we only need
it once after the javascript setup of the links.

All thats left to do now is to hook up the event handler with the event in the followin code block that lives inside From.Design.cs: InitializeComponent():

this.webBrowser1.StatusTextChanged += new System.EventHandler(webBrowser1_StatusTextChanged);


Please tell me what you think about this "special" solution.

Cheers

Manfred
 
Share this answer
 
v6
Thanks for the quick reply. but If i may comment, the problem is not with the html page itself. Is with the application. can i get it? if so how or most importantly when? after everything has loaded?
 
Share this answer
 
Comments
Manfred Rudolf Bihy 16-Nov-10 10:09am    
Worked out a "solution" (aka hack) and edited my first answer to show how I got it to work.
Sandeep Mewara 16-Nov-10 11:16am    
Use 'Add Comment' feature to respond to an answer. Upvote/Mark as answer accepted if it helps/resolves your issue.
Wow, your hack works. Sorry but I got one last problem though.

I see the hack you applied on the browser event handler, but the problem is that the page title is set on the JavaScript. and that statement is only evaluated after the script has generated the hyper link. what if the title does not change? like it's set in the page itself. then JavaScript would have nothing todo with the title. how will i know that the browser is done evaluating the script since they will be the last one to be outputted? Thanks in advance
 
Share this answer
 
Comments
Manfred Rudolf Bihy 16-Nov-10 12:01pm    
This should be a comment to "Answer 1". If you keep answering (answering yourself does indeed seem a little strange ;) ) instead of commenting I will not receivee notifications thatsomeone responded to my answer.
Manfred Rudolf Bihy 16-Nov-10 12:03pm    
Back to your question: What does page title have to do with my hack? I'm working with StatusText to do all the magic and the title of your document has nothing to do with it. Please explain?
mshauny 16-Nov-10 12:14pm    
Oh sorry. I'm not very familiar with codeproject's system.
mshauny 16-Nov-10 12:53pm    
I don't know if you got my last reply.

But here were my concerns.
1. The page title is set by JavaScript script (which is evaluated last on the page output).
--- My page uses static title. (which is not affected by JS in anyway ) just when the broswer request a page from the server, my title is constructed as "Qustions... | questions.php?qs='question #number'"

2. The web browser StatusText property is set by reading, the current page title. and "If the JS script has been evaluated then obviously we now have a page title (which we did not have before) then that means our webbrowser now has the StatusText property filled with the page title.

3. I already had the page title right from the moment my webbroswer sent a request to the server for a new page.
---That means if i subscribe to an event handler that checks if the StatusText has changed and put the code inside to extract links from it. Then i have 2 problesms.
1. The page title + statustext has not chnaged and never will change fro that page
2. My if part where is test if my StatusText== "something" will always be true becuase the page title is the same.

All i need is a way to know that the page is done loading, all javascript scripts are done executing, and that no further connection is open to the server (this is true because my page does not use AJAX features)

Is that possible?
Manfred Rudolf Bihy 16-Nov-10 16:02pm    
I'll have to clear this up. Part of the hack is setting the status text via javascript to something well defined that is recognized by the event handler as a sort of signal that the dynamic generation of content is now complete. Then in the event handler after we did the stuff we wanted to do (inside the if that recognizes end of javascript execution) the status text is changed to something that does not equal the signaling text, this may well be the page title if you like. If you still have issues can you post a short concise html document that illustrates your concerns? It would probably help me understand what you listed in your points 1. through 4.
Oh my god! I just found this by googling around some:

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting(v=VS.80).aspx

Forget the hack and do it the way described in the sample on the page above. Works from .NET 2.0 to .NET 4.0.

Don't we both wish I'd found this earlier. :)

Never the less, it's of course also satisfieing when one finds a viable workaround.

Cheers

Manfred
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900