js获取网页域名,js获取当前url域名

发布时间:2023-12-08

js获取网页域名,js获取当前url域名

更新:2022-11-18 10:57

本文目录一览:

  1. 两种js获取当前域名代码
  2. 用JS获取当前域名并判断
  3. 如何使用Javascript获取一个链接地址中的顶级域名
  4. 如何通过js获取当前访问页面的域名

两种js获取当前域名代码

今天给各位朋友介绍两种js获取当前域名。 代码如下:

// 获取当前域名
1、window.location.host;
2、document.domain;
// 获取当前页面地址
url = window.location.href;

例子:

// 获取域名
host = window.location.host;
host2 = document.domain;
// 获取页面完整地址
url = window.location.href;
document.write("brhost=" + host);
document.write("brhost2=" + host2);
document.write("brurl=" + url);

补充:获取当前域名信息

thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
strwrite = "thisTLoc:[" + thisTLoc + "]";
strwrite += "thisPLoc:[" + thisPLoc + "]";
strwrite += "thisTHost:[" + thisTHost + "]";
strwrite += "thisHost:[" + thisHost + "]";
document.write(strwrite);

用JS获取当前域名并判断

在页面插入js代码:

function loadFN() {
    hrefValue = window.location.href; // 获取当前页面的地址
    alertUrls = ['']; // 指定你想要alert的域名,多个可以在数组中直接追加即可
    // 追加如:['', 'bbb.com', 'abc.bbb.com']
    for (key in alertUrls) {
        if (String(hrefValue).indexOf(alertUrls[key]) == 0) {
            // 指定的字符串值alertUrls[key]在字符串href中出现则=0,否则为-1
            alert(alertUrls[key]);
            break; // 直接跳出循环
        }
    }
}
// 页面载入完成后即执行loadFN函数
window.onload = loadFN; // 前提是原来页面没有使用onload,否则还要做些针对的处理

如何使用Javascript获取一个链接地址中的顶级域名

<a id="a1" href="" target="_blank">JS特效</a>
alert(getdomain(1,'a1')); // 弹窗输出域名
function getdomain(typ, id) { // 参数:类型(0:当前域名,1:顶级域名),链接对象ID
    var url = window.document.getElementById(id).href; // 获取链接
    if (typ == 0) { // 获取当前域名,如:
        var a = document.createElement('a');
        a.href = url;
        url = a.hostname;
    } else { // 获取顶级域名,如:lingchenliang.com
        // 使用正则表达式
        url = url.replace(/.+[\.\/]([A-z]+\.[A-z]+)\/[^\/].+/,"$1");
    }
    return url; // 返回域名值
}

如何通过js获取当前访问页面的域名

<input type="text" style="width:300px;" name="new" id="new">
var nurl = document.referrer; // 来源url
document.URL; // 获取当前域名
document.title; // 获取当前页面标题
document.getElementById('new').innerHTML = nurl;

我是爱分享资源网的站长,如果你觉得不错请访问下我的网站,谢谢了!