0

I want to try to a lib ala jquery but I don't understand why my method getUrl doesn't output anything, do you ?

mylib.js

    (function () {

    var scripts = document.getElementsByTagName('script');
    var index = scripts.length - 1;
    var thisScript = scripts[index];

    var myLib = {       
        getUrl: function() {
            return thisScript;
        }               
    }
}

if (!window.$$) {window.$$ = myLib}

)();

and in mytest.html

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.js"></script>
    <script src="mylib.js" type=text/javascript></script>
</head>

<body>
    <DIV id=url>url</DIV>   
    <script>
    var url = $$.getUrl();
    jQuery("#url").html(url);
    </script>
</body>
</html>

1 Answer 1

5

Your code had a misplaced }.

(function() {
    var scripts = document.getElementsByTagName('script');
    var index = scripts.length - 1;
    var thisScript = scripts[index];

    var myLib = {       
        getUrl: function() {
            return thisScript;
        }               
    };
//} <-- Remove this. 

  if (!window.$$) {window.$$ = myLib;}
} // <-- Add this, the closing brace of the anonymous function
)();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.