简体   繁体   中英

How do I get the link attributes (page name) in the “Webbrowser” event when I click a link?

I plan to make a content management system.
System composition:
- The page tree by category. (Page tree by category);
- Web browser. (webbrowser);

Contents of the pages:
- formatted text;
- Pictures;
- references;

Text in HTML format is stored in the database.
The category tree displays text from the database to the Web browser.

It is necessary to make a transition between pages.

That is, the transition from page "Page_1" to the necessary paragraph of the page "Page_2" Logics:
- User. It is on page "Page_1".
- User. Click the link on the page "Page_1".
- Browser. Opens the page "Page_2", scrolls to "Paragraph_N" ("N" depends on which paragraph on the page is "Page_1")

The transition between the pages I plan to do with links "anchor"

<a href="text.html#bottom"> Go to the bottom of the text </a>  

I think that to perform this logic, need the application to do the following:
- track the event "clicking on the link";
- Extract from the link the name of the page ("text.html") referenced by the link;
- extract the page from the database according to the name;

Example HTML code "Page1" and "Page 2"
https://codeshare.io/2Bb03L

Questions
1. How do I get the link attributes (page name) in the "Webbrowser" event when I click a link?
2. What are the more rational ways to perform this logic (go from page "Page_1" to page "Page_2")?
3.0 How to make the following actions when the link is clicked:
3.1. If the link to the site - open in Chrome?
3.2. If the link to the page from the database - open in "webBrowser1"?
3.3. If the link to the paragraph of pages from the database - open in
"webBrowser1" and scroll the scroll to the desired paragraph?
image

Answering your first question " How do I get the link attributes (page name) in the "Webbrowser" event when I click a link? ":

Assuming your Webbrowser is called webBrowser1 :

You need expose a C# object to the WebBrowser that the JavaScript can call directly, the object must have the ComVisibleAttribute set true.

[System.Runtime.InteropServices.ComVisible(true)]
public class ScriptInterface
{
    public void OnAnchorClick(string href)
    {
        var parts = href.Split('#');
        var pageName = parts[0];
        //do whatever with page name    
    }
}

Configure your Webbrowser:

webBrowser1.ObjectForScripting = new ScriptInterface();

Now in your js you can call the OnAnchorClick method this way:

document.getElementsByTagName('a')[0].onclick = function(){
   window.external.OnAnchorClick(this.href);
   return false;
}

Other usefull references:

how-to-handle-javascript-events-via-webbrowser-control-for-winforms

how-to-inject-javascript-in-webbrowser-control

calling-javascript-in-a-webbrowser-control-from-c#

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM