问题
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