Created
February 21, 2018 03:42
-
-
Save leadVisionary/b54681aed83f3e6391fec8ba44c6cb83 to your computer and use it in GitHub Desktop.
First Attempt at ECMAScript modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function crossPlatformSpanTextUpdate(span, text) { | |
if ('textContent' in span) { | |
span.textContent = text; | |
} else { | |
span.innerText = text; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>An E-commerce retailer</title> | |
</head> | |
<body> | |
<h1> | |
Hello <span id="greeting">Placeholder</span>!</h1> | |
<h2> | |
Please <a href="https://www.blogger.com/buy/12345">buy</a> this product</h2> | |
</body> | |
<script src="greeting.js" type="module"></script> | |
<script type="text/javascript"> | |
document.onready = greetCustomer(document.querySelector("#greeting")); | |
</script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {getUserPreferredLanguage} from './language.js' | |
import {crossPlatformSpanTextUpdate} from './cross_platform_span_update.js' | |
export default function greetCustomer(location) { | |
loadGreeting(location, getUserPreferredLanguage()); | |
} | |
function loadGreeting(span, lang) { | |
var text = greeting(lang); | |
crossPlatformSpanTextUpdate(span, text); | |
} | |
function greeting(lang) { | |
if (lang === "es") { | |
return "Dear dear Customer"; | |
} | |
return "Valued Shopper"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
used for language detection from | |
https://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference | |
*/ | |
export default function getUserPreferredLanguage() { | |
return window.navigator.languages[0] || | |
[window.navigator.language || window.navigator.userLanguage]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment