-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddTextBasedOnHttpOrHttps.js
29 lines (27 loc) · 1.06 KB
/
addTextBasedOnHttpOrHttps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// I expect to import this code into my projects and have it run right away.
// Import it like this:
//<script src='https://rawgit.com/hchiam/javascriptSandbox/master/addTextBasedOnHttpOrHttps.js'></script>
addTextBasedOnHttpOrHttps();
function addTextBasedOnHttpOrHttps() {
let thisUrl = document.URL;
if (thisUrl.includes("https")) {
// get http link
let urlAsHttpNotHttps = thisUrl.substring(0, 4) + thisUrl.substring(5);
// create text to add
let addedText = document.createElement("p");
addedText.id = "addTextBasedOnHttpOrHttps";
addedText.appendChild(
document.createTextNode("This page/app not working? Try ")
);
// add link to that text
let aLink = document.createElement("a");
aLink.href = urlAsHttpNotHttps;
aLink.innerHTML = "http";
aLink.target = "_blank";
addedText.appendChild(aLink);
addedText.appendChild(document.createTextNode(" instead of https."));
document.body.insertBefore(addedText, document.body.firstChild);
// center the added text
addedText.setAttribute("align", "center");
}
}