Capitalizing a domain name in a URL using Javascript

风流意气都作罢 提交于 2020-01-05 07:11:23

问题


Take a look at the following code:

<!--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-->

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

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?

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.


回答1:


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.



来源:https://stackoverflow.com/questions/12551517/capitalizing-a-domain-name-in-a-url-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!