Load a URL (Excluding Header and Footer)

I am trying to load a URL in webView, and have it exclude the website Header and Footer from displaying within the view.

Answered by jkhkhbjhbj in 794836022

To exclude the header and footer of a website from displaying within a WKWebView, you can manipulate the loaded web content using JavaScript injection. Here's an example of how you can achieve this:

import UIKit import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView = WKWebView(frame: view.bounds)
    webView.navigationDelegate = self
    view.addSubview(webView)

    let url = URL(string: "https://sairam.com") 
    let request = URLRequest(url: url!)
    webView.load(request)
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    // Inject JavaScript to hide the header and footer
    let script = """
    var header = document.querySelector("header"); // Replace "header" with the appropriate selector for your website's header
    var footer = document.querySelector("footer"); // Replace "footer" with the appropriate selector for your website's footer

    if (header) {
        header.style.display = "none";
    }

    if (footer) {
        footer.style.display = "none";
    }
    """
    webView.evaluateJavaScript(script, completionHandler: nil)
}

}

change it according to your code. Here, JavaScript is injected to hide the header and footer elements using the with appropriate CSS selector.Also identify the appropriate selectors for the header and footer elements you can inspect the web page's source code or use browser developer tools.

username : Sairam Agireeshetti

Accepted Answer

To exclude the header and footer of a website from displaying within a WKWebView, you can manipulate the loaded web content using JavaScript injection. Here's an example of how you can achieve this:

import UIKit import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView = WKWebView(frame: view.bounds)
    webView.navigationDelegate = self
    view.addSubview(webView)

    let url = URL(string: "https://sairam.com") 
    let request = URLRequest(url: url!)
    webView.load(request)
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    // Inject JavaScript to hide the header and footer
    let script = """
    var header = document.querySelector("header"); // Replace "header" with the appropriate selector for your website's header
    var footer = document.querySelector("footer"); // Replace "footer" with the appropriate selector for your website's footer

    if (header) {
        header.style.display = "none";
    }

    if (footer) {
        footer.style.display = "none";
    }
    """
    webView.evaluateJavaScript(script, completionHandler: nil)
}

}

change it according to your code. Here, JavaScript is injected to hide the header and footer elements using the with appropriate CSS selector.Also identify the appropriate selectors for the header and footer elements you can inspect the web page's source code or use browser developer tools.

username : Sairam Agireeshetti

This is awesome!!!! It worked perfectly. Thanks so much!

Thank you for the update.

Regards Sai ram Agireeshetti

Load a URL (Excluding Header and Footer)
 
 
Q