使用 Javascript 将 URL 中的域名大写 [英] Capitalizing a domain name in a URL using Javascript

查看:40
本文介绍了使用 Javascript 将 URL 中的域名大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看下面的代码:

<!--begin of index.html-->

<a href="#" id='click'>Click to change url to Index2.html</a>

<script type="text/javascript">
    var stateObj = { foo: "bar" };
    function change_my_url()
    {
       history.pushState(stateObj, "page 2", "http://MyDomain.com/Index2.html");
    }
    var link = document.getElementById('click');
    link.addEventListener('click', change_my_url, false);
</script>

<!--end of index.html-->

这是在不重新加载页面的情况下更改 URL 的示例脚本.

This one is the sample script to change the URL without reloading the page.

脚本成功更新了来自 http://mydomain.com/index.htmlhttp://mydomain.com/Index2.html 无需重新加载页面.但是,尽管在更新的 URL 中,文件名如代码中所述大写,但域名未大写.我希望 URL 看起来像 http://MyDomain.com/Index2.html.这可能吗?

The script successfully updates the URL from http://mydomain.com/index.html to http://mydomain.com/Index2.html without reloading the page. But, though in the updated URL, the filename is capitalized as mentioned in the code, the Domain name is not capitalized. I want the URL to look like http://MyDomain.com/Index2.html. Is this possible?

我知道我认为此脚本将大写域名的假设是错误的.但我只是举了一个例子,说明我在 使用 Javascript 在 URL 中将域名大写.

I know that my assumption that this script will capitalize domain name is wrong. But I just gave you an example of my efforts to Capitalizing a domain name in a URL using Javascript.

推荐答案

域名不区分大小写,但约定只使用小写.这就是为什么您的浏览器总是将您提供的域用大写字母重写为小写.如果没有这个约定,我们就会有不同的字符串是相同的 URL,这会令人困惑.

Domain names are case insensitive but the convention is to use only lowercase. That's why your browser will always rewrite in lowercase the domain your provide with uppercase letters. Without this convention we would have different strings being the same URL, which would be confusing.

这篇关于使用 Javascript 将 URL 中的域名大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆