I was wondering how to change a url in javascript
for example if i wrote an address

How would i get the script to change the address to
www.google.ie

Dani AI

Generated

A small, robust userscript pattern that addresses both cases raised in the thread: changing anchor hrefs in the page and optionally redirecting the current page if it uses the wrong TLD. 's inline onclick works only on click and is brittle; 's redirect idea works but can lose path/query and creates a back-button history entry. The snippet below preserves path/query/hash, only touches HTTP(S) URLs, and updates anchors added dynamically.

// ==UserScript==
// @name        Fix wrong-TLD links
// @match       *://*/*
// @run-at      document-start
// ==/UserScript==

(function(){
  'use strict';

  function replaceHostIfEi(href){
    try{
      const url = new URL(href, location.href);
      if ((url.protocol === 'http:' || url.protocol === 'https:') && /\.ei$/i.test(url.hostname)){
        url.hostname = url.hostname.replace(/\.ei$/i, '.ie');
        return url.toString();
      }
    }catch(e){
      // fallback for older engines: parse with an <a> element
      try{
        const a = document.createElement('a');
        a.href = href;
        if ((a.protocol === 'http:' || a.protocol === 'https:') && /\.ei$/i.test(a.hostname)){
          const newHost = a.hostname.replace(/\.ei$/i, '.ie');
          return a.protocol + '//' + newHost + a.pathname + a.search + a.hash;
        }
      }catch(e2){}
    }
    return null;
  }

  if (/\.ei$/i.test(location.hostname)){
    const newUrl = replaceHostIfEi(location.href);
    if (newUrl) location.replace(newUrl); // avoids creating a history entry
    return;
  }

  function fixAnchors(root){
    (root || document).querySelectorAll('a[href]').forEach(a=>{
      const updated = replaceHostIfEi(a.href);
      if (updated) a.href = updated;
    });
  }

  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', ()=>fixAnchors(document));
  else fixAnchors(document);

  new MutationObserver(m=>m.forEach(r=>r.addedNodes && r.addedNodes.forEach(n=>{ if (n.nodeType===1) fixAnchors(n); })))
    .observe(document.documentElement || document, {childList:true, subtree:true});
})();

Notes: match only the hostname (so you do not accidentally change other occurrences of "ei"), use location.replace() to avoid a history entry, check protocol so mailto: or javascript: links are ignored, and use a MutationObserver for pages that inject links after load.

Recommended Answers

All 2 Replies

It's either you create a function that will handle your specified URLs or you can just apply simple inline execution like this:

<a onclick="javascript: this.href = this.href.replace('ei', 'ie');" href="www.google.ei" >Changing URL with JavaScript!</a>

It's pretty easy, in your userscript put these lines:

var regExp = /google\.ei\//i;
if (regExp.test(window.location.href))
        window.location.href = "http://www.google.ie";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.