I want to make it so that for example if I'm on myownwebsite.com when it loads I want the URL to change but not visit the link just have it say the URL I want in there for example mysecondwebsite.com is this possible?
-
possibility to duplicate https://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page – Anouar khaldi Jun 04 '18 at 22:08
-
Or... https://gomakethings.com/how-to-update-a-url-without-reloading-the-page-using-vanilla-javascript/ – RandomUs1r Jun 04 '18 at 22:11
-
@RandyCasburn I think you picked the wrong history api there. :) https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState()_method – Keith Jun 04 '18 at 22:35
-
Whoops! thanks @Keith! – Randy Casburn Jun 04 '18 at 22:36
3 Answers
Your particular example isn't possible with JavaScript only. The history API does publish a method called pushState()
, which allows you to change the URL displayed in the address bar, but -- like so many other JavaScript features -- it is bound by the same-origin policy. In this case, you cannot change the address to a URL that has a different origin (i.e. hostname + port number) than the currently loaded page. Otherwise, it would be way too easy for malicious websites to pose as Google, Facebook, etc.

- 4,081
- 1
- 10
- 22
This doesn't exactly answer your question as it is not javascript, but would a DNS CNAME record work in your case?
quote: "A Canonical Name record (abbreviated as CNAME record) is a type of resource record in the Domain Name System (DNS) used to specify that a domain name is an alias for another domain (the 'canonical' domain)."

- 1,307
- 1
- 13
- 25
No, it is impossible.
The browser won't authorize you to "cheat" on users.
If the URL is the second website's URL, it should show the second website's content.
What if you make the second website show the first website's content while you receive a certain query parameter?

- 1,521
- 8
- 16