Problem:
I need to a redirect to a calendar url that includes month and year variables. Is this viable in javascript?
<script>
const currentDate = new Date();
const m = currentDate.getMonth();
const y = currentDate.getFullYear();
const myUrl = `www.somewhere.com/events.htm?pageComponentId=1234&month=${m}&year=${y}`;
document.getElementById("currentDate").innerHTML = myUrl;
document.location.assign(myUrl);
</script>
Everything works except the document.location.assign(myUrl) I have tried various ways to reference the variable myUrl but I clearly haven’t wrapped my head around how to properly do this. Your help is appreciated.
Solution:
All valid URLs follow a specific pattern: protocol, domain name, and path. In your URL the protocol is missing.
As Barmar
mentioned, It will be interpreted as a subdirectory/path relative to the current URL.
// Add http or https
const myUrl = `https://www.somewhere.com/events.htm?pageComponentId=1234&month=${m}&year=${y}`;
Example
If the current URL is https://example.com/something
, Then document.location.assign("www.bingo.com");
will take you to https://example.com/www.bingo.com
.