HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Generated social media links

Submitted by: @import:stackexchange-codereview··
0
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 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.