三三世界-百宝箱

文章 分类 标签
130 9 247

公告

暂无站点公告

相同网页调用当前访问域名并输出的方法

三三 2024-03-30 109 0条评论 网站建设 相同网页调用不同的域名scrpit应用

首页 / 正文

应用场景:

不同的域名对应相同的网页,想要在某一个区域显示出当前访问的域名。

例如:

Copyright © 2024. www.33f.net All rights reserved. 

解决方法:

在head区域加入script代码如下:

<script>
window.onload = function() {
    // 获取当前网址
    var currentUrl = window.location.href;

    // 提取网址中的域名部分
    var domain = currentUrl.split('/')[2];

    // 输出当前年份到一个<span>元素
    document.getElementById("currentYear").innerHTML = new Date().getFullYear();

    // 输出当前网址的域名部分到一个<span>元素
    document.getElementById("currentUrl").innerHTML = domain;
};
</script>

在输出内容区域加入如下代码:

<span id="currentYear"></span>
<span id="currentUrl"></span>

用这个方法,你访问的是哪个域名,就显示出哪个域名。 具体的操作,比如加连接之类的请自行扩展。

方法2:

还有一个方法,就是在head区域加入script:

<script>
var 33f = window.location.host;
</script>

输出区域输入:

<script>document.write(33f);</script> 

这样就可以直接输出当前网址,更简单。但是这个也带来一个问题就是兼容性问题,虽然简单,但是可能某些网页无法正常的显示或者覆盖内容等。不利于维护。

也可以试着这样输出
head区域上方:

<script>
window.onload = function() {
    // 获取当前网址的域名部分
    var domain = window.location.host;

    // 创建一个新的文本节点
    var textNode = document.createTextNode(domain);

    // 找到页面上的一个元素,例如一个<div>元素,并将文本节点添加到该元素中
    document.getElementById("domainOutput").appendChild(textNode);
};
</script>

内容输出区域:

<span id="domainOutput"></span>

根据自己的情况进行选择操作。

评论(0)

当前没有评论,还不快来留下第一个脚印吧


Copyright 2021 三三世界-百宝箱. All Rights Reserved.

最新评论

  • 三三

    @Doug Shume it's ok for me , you can post here.

  • refugiaguenther

    Saved as a favorite, I like your website!

  • josefa

    If some one wishes to be updated with hottest technologies after that he must be visit this site and be up to date daily.

  • Penzu

    Heello would you mind sharing which blog platform you're using? I'm planning to start my own blog in the near future but I'm having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then moost blogs and I'm looking for something completely unique. P.S Apologies forr being off-topic butt I had to ask!

  • Yupoo Fendi

    Thanks to my father who shared with me regarding this webpage, this website is genuinely amazing.

  • Doug Shume

    Hi, I have an overflow of customers that I'd like to send to you but I want to make sure you can handle more leads, let me know if you'd like me to send you more info.

  • SuperWind

    zh.us.to 有效
    kms.03k.org 有效
    kms.chinancce.com
    kms.shuax.com 有效
    kms.dwhd.org 有效
    kms.luody.info 有效
    kms.digiboy.ir 有效
    kms.lotro.cc 有效
    www.zgbs.cc 有效
    cy2617.jios.org 有效

  • 三三

    @         权限问题,试试sudo 再加命令。

  •         

    你好提示Permission denied 怎么办啊

日历

2024年05月

   1234
567891011
12131415161718
19202122232425
262728293031 

文章目录

上一张 下一张