Recently I've been running into issues with the Web-browser control in C#.
Navigating to web pages with JavaScript implemented in them makes the WebBrowser crash - It claims there is a syntax error in JS code that is in that webpage. ( It provides the line and what char is the error in so i went to see what caused the error ).
example for an error :
function validate_fen(fen) {
var errors = {
0: 'No errors.',
1: 'FEN string must contain six space-delimited fields.',
2: '6th field (move number) must be a positive integer.',
3: '5th field (half move counter) must be a non-negative integer.',
4: '4th field (en-passant square) is invalid.',
5: '3rd field (castling availability) is invalid.',
6: '2nd field (side to move) is invalid.',
7: '1st field (piece positions) does not contain 8 \'/\'-delimited rows.',
8: '1st field (piece positions) is invalid [consecutive numbers].',
9: '1st field (piece positions) is invalid [invalid piece].',
10: '1st field (piece positions) is invalid [row too large].',
};
the line the error is in is 3977 char number 5 or more specifically last line in this code in char 5 ( Which means its probably the last comma , Correct me if I'm wrong ).
more specific code for the error:
10: '1st field (piece positions) is invalid [row too large].',
};
Given the fact that other browsers do load the website properly including IE ( I'm aware of the fact that the webBrowser and IE aren't the same ) How will I approach to solving these problems?
My thoughts were:
I'm not sure but I think the webBrowser control doesn't load the script if there is an error which causes more errors later on.
How will I approach on making loading a specific webpage successful? Is it possible to edit the script while in running (The script isn't in the head scetion of the HTML , its on the server)? And if possible , how?
How to load and fix scripts in C#:
Copy the non loading script from the website and fix the syntax errors and copy it to your code.
Important note: put all of the script's code in a function so you can call it later
Use the following code to add script to site:
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = script;//script is your script which you defined
head.AppendChild(scriptEl);
call InvokeScript to the function you created.
If you fixed all the syntax errors it should be fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.