patternjavascriptMinor
Generated social media links
Viewed 0 times
linkssocialgeneratedmedia
Problem
I'm still fairly new with JavaScript, and wrote something to generate some social media links on my page. The idea was to have that script grab the necessary URL info and feed it to the social media APIs.
// print links
titleElement = urlencode(document.title);
linkElement = urlencode(location.href);
document.write ("");
document.write ("");
document.write ("");
document.write ("");
document.write ("");
Solution
Use the
Using
You should still take the advice of @NebulaFox - don't use
var keyword to declare titleElement and linkElement in this scope, otherwise they will always be global variables and you'll have no choice about it.Using
var will still put them in the global scope until you wrap this logic in a closure:(function() {
// var titleLink...
})();You should still take the advice of @NebulaFox - don't use
document.write.Code Snippets
(function() {
// var titleLink...
})();Context
StackExchange Code Review Q#4825, answer score: 2
Revisions (0)
No revisions yet.