开发者

missing } after function body... even though everything is closed properly

I'm trying to make a bookmarklet that first checks for jQuery on a page and loads the library if necessary, before loading my custom script. The script looks like this:

javascript:(function() {
  function loadMyScript() {
    var my_src = document.createElement('script');
    my_src.src='url_of_my_script';
    document.body.appendChild(my_src);
  }
  function loadJQuery() {
    var otherlib = false;
    if (typeof $ === 'function') {
      otherlib = true;
    }
    function getScript(url, success) {
      var script = document.createElement('script');
      script.src = url;
      script.onload = success;
      document.body.appendChild(script);
      if (otherlib) {
        jQuery.noConflict();
      }
    }
    getScript('http://code.jquery.com/jquery-latest.min.js', loadMyScript);
  }
  if (typeof jQuery === 'undefined') {
    loadJQuery();
  } else {
    loadMyScript();
  }
})();

Putting this code in a bookmarket in Firefox 4, I get "missing } after function body" when I try to run it. However, running the same code in the Firebug console succeeds. Chrome yields similar results; the bookmarklet spits out "Unexpected end of input", while running the code in the console succeeds.

Anyone knows what's happening here?

(Aside: the getScript function is somewhat incomplete. I just ripped out a few t开发者_如何学JAVAhings to try and solve this syntax problem)


my_src.src = 'url_of_my_script'

That line is missing a semi-colon. That was the only error I received.


When you are doing bookmarklets...

Use block-style comments instead of single-line comments

function () {

  /*Right:*/
  return 0;

  //Wrong:
  return 0;
}

After wrapping to a single-line bookmarklet code, the above becomes:

function () { /*Right:*/ return 0; //Wrong: return 0; }

Check for missing semicolons

Missing semicolons are okay as long as no statement follows them. For simplicity, it's best to always use them in a bookmarklet.

function () {

  /*Right:*/
  var a = 0;

  /*Wrong:*/
  a = 0

  /*Doesn't matter:*/
  a = 0
}

The above becomes:

function () { /*Right:*/ var a = 0; /*Wrong:*/ a = 0 /*Doesn't matter:*/ a = 0 }


Try moving the invocation parentheses inside the parentheses that cover the function.

Like this:

(function(){
   // do stuff
}())


I was having a similar problem - strange Javascript error on a very basic bit of code. Stranger still was that the code worked fine locally (Win7) but did not run on my server (Ubuntu).

On a hunch I copied the text inside of the .js file, and pasted into a different text editor (TextPad) than my IDE (NetBeans) and saved the file. This time when I uploaded the file there was no Javascript error.

For some reason, the original file had been getting its line breaks stripped.


I think the problem is on the first line :

javascript:(function() {

here, you have two choices :

even you are on a object and you do something like :

var myobj = {
    javascript: function () {
        // code here
    }
}

or you create a function :

var func_name = function () {
    // code here
}

or, eventually, something like this :

;(function($) {func_name = function (sBind, oFunction) {
    // code here
}})();


You're missing a semi-colon at the end of the following line:

my_src.src='url_of_my_script'


The inner function defined in loadJQuery was tripping things up. I changed things around like so:

javascript:(function() {
  function loadMyScript() {
    var my_src = document.createElement('script');
    my_src.src = 'url_of_my_script';
    document.body.appendChild(my_src);
  }
  function getScript(url, success) { // <- moved this function outside loadJQuery
    var script = document.createElement('script');
    script.src = url;
    script.onload = success;
    document.body.appendChild(script);
  }
  function loadJQuery() {
    var otherlib = false;
    if (typeof $ === 'function') {
      otherlib = true;
    }
    getScript('http://code.jquery.com/jquery-latest.min.js', function() {
      if (otherlib) {
        jQuery.noConflict();
      }
      loadMyScript();
    });
  }
  if (typeof jQuery === 'undefined') {
    loadJQuery();
  } else {
    loadMyScript();
  }
})();

This is new to me. Are nested functions not allowed in bookmarklets? I have yet to see any documentation on this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜