简体   繁体   中英

GitHub Pages display custom domain in address bar

How can I display a custom domain in the address bar of my GitHub Pages site. It is a shared domain name so CNAME records are restricted.

Is there any way I can edit my JS/HTML code such that it displays my domain name in the address bar?

I tried window.location.href = "www.mysite.site.com"; But that results in the address being username.github.io/www.mysite.site.com .

Thank you.

You cannot change the domain of a website via javascript, if you could, that would be a security vulnerability .

Imagine having the power to make any website appear to be any other website.

window.location.href only changes the path after the domain

Use http:// or https:// in the domain.
As: window.location.href = "https://mysite.site.com";
And this will redirect to https://mysite.site.com and not username.github.io/www.mysite.site.com .

And if you only want the browser to change the address and do not reload, then you can use:

<body onload="ChangeUrl('Title', '/page-adress.html');">
<script type="text/javascript">  
function ChangeUrl(title, url) {  
    if (typeof(history.pushState) != "undefined") {  
        var obj = { Title: title, Url: url };  
        history.pushState(obj, obj.Title, obj.Url);  
    } else {  
        alert("Browser does not support HTML5.");  
    }  
}  
</script> 


You can also use :
 <body onload="function1"> <script> function function1() { location.replace("https://www.w3schools.com") } </script>

It also redirects but disables the back button to navigate to the last page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM