How to create web browser using HTML


Here is the code snippet

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Advanced Web Browser</title>
  <style>
    /* General Styles */
    body {
      font-family: 'Arial', sans-serif;
      margin: 0;
      padding: 0;
      display: flex;
      flex-direction: column;
      height: 100vh;
      background: #f0f4f8;
    }

    /* Address Bar Styles */
    #address-bar {
      display: flex;
      padding: 10px;
      background: linear-gradient(to right, #6a11cb, #2575fc);
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
      gap: 8px;
      margin:5px;
    }

    #url-input {
      flex: 1;
      border: none;
      border-radius: 5px;
      padding: 10px;
      font-size: 16px;
      box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.1);
    }

    #url-input:focus {
      outline: 2px solid #2575fc;
    }

    #go-button {
      background-color: white;
      border: none;
      border-radius: 5px;
      padding: 8px 15px;
      font-size: 14px;
      font-weight: bold;
      color: #2575fc;
      cursor: pointer;
      transition: all 0.3s ease-in-out;
    }

    #go-button:hover {
      background-color: #2575fc;
      color: white;
      transform: scale(1.1);
    }

    /* Navigation Bar Styles */
    #nav-bar {
      display: flex;
      justify-content: center;
      padding: 8px;
      background: #ffffff;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
      gap: 10px;
    }

    #nav-bar button {
      background-color: #2575fc;
      border: none;
      border-radius: 5px;
      padding: 8px 15px;
      font-size: 14px;
      color: white;
      cursor: pointer;
      transition: all 0.3s ease-in-out;
    }

    #nav-bar button:hover {
      background-color: #6a11cb;
      transform: scale(1.1);
    }

    #nav-bar button:disabled {
      background-color: #e0e0e0;
      color: #a0a0a0;
      cursor: not-allowed;
    }

    /* Web View (iframe) */
    #web-view {
      flex: 1;
      border: none;
      width: 100%;
      height: 100%;
      box-shadow: inset 0 5px 15px rgba(0, 0, 0, 0.1);
    }
  </style>
</head>
<body>
  <!-- Address Bar -->
  <div id="address-bar">
    <input type="text" id="url-input" placeholder="Enter URL (e.g., https://www.example.com)">
    <button id="go-button">Go</button>
  </div>

  <!-- Navigation Bar -->
  <div id="nav-bar">
    <button id="back-button" disabled>&larr; Back</button>
    <button id="forward-button" disabled>&rarr; Forward</button>
    <button id="refresh-button">⟳ Refresh</button>
  </div>

  <!-- Web View -->
  <iframe id="web-view" src=""></iframe>

  <script>
    const urlInput = document.getElementById('url-input');
    const goButton = document.getElementById('go-button');
    const backButton = document.getElementById('back-button');
    const forwardButton = document.getElementById('forward-button');
    const refreshButton = document.getElementById('refresh-button');
    const webView = document.getElementById('web-view');

    let historyStack = []; // Tracks visited URLs
    let currentIndex = -1; // Tracks the current position in the history stack

    // Function to update navigation buttons
    const updateNavButtons = () => {
      backButton.disabled = currentIndex <= 0;
      forwardButton.disabled = currentIndex >= historyStack.length - 1;
    };

    // Navigate to a URL
    const navigateTo = (url) => {
      if (!url.startsWith('http://') && !url.startsWith('https://')) {
        url = 'https://' + url;
      }
      webView.src = url;

      // Update history stack
      if (currentIndex === historyStack.length - 1) {
        historyStack.push(url);
      } else {
        historyStack = historyStack.slice(0, currentIndex + 1);
        historyStack.push(url);
      }
      currentIndex++;
      updateNavButtons();
    };

    // Handle Go button click
    goButton.addEventListener('click', () => {
      const url = urlInput.value;
      navigateTo(url);
    });

    // Handle Back button click
    backButton.addEventListener('click', () => {
      if (currentIndex > 0) {
        currentIndex--;
        webView.src = historyStack[currentIndex];
        updateNavButtons();
      }
    });

    // Handle Forward button click
    forwardButton.addEventListener('click', () => {
      if (currentIndex < historyStack.length - 1) {
        currentIndex++;
        webView.src = historyStack[currentIndex];
        updateNavButtons();
      }
    });

    // Handle Refresh button click
    refreshButton.addEventListener('click', () => {
      if (currentIndex !== -1) {
        webView.src = historyStack[currentIndex];
      }
    });

    // Handle Enter key press in the URL input
    urlInput.addEventListener('keypress', (event) => {
      if (event.key === 'Enter') {
        navigateTo(urlInput.value);
      }
    });

    // Update URL input when iframe finishes loading
    webView.addEventListener('load', () => {
      urlInput.value = webView.src;
    });

    // Initial state
    updateNavButtons();
  </script>
</body>
</html>

Post a Comment

Previous Post Next Post

Ad01

Ad02