admin管理员组文章数量:1279175
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('.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 inplete. I just ripped out a few things to try and solve this syntax problem)
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./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 inplete. I just ripped out a few things to try and solve this syntax problem)
Share Improve this question edited Apr 6, 2011 at 0:55 curagea asked Apr 5, 2011 at 18:46 curageacuragea 872 silver badges5 bronze badges 2-
1
I'm sure it's not the problem, but you don't have a semi-colon after
my_src.src='url_of_my_script'
. I'd correct that just for consistency :) – Jon Skeet Commented Apr 5, 2011 at 18:49 - 1 That leading "javascript:" is unnecessary. edit oh wait except for that extremely important word "bookmarklet" in your question :-) Sorry about that; never mind. – Pointy Commented Apr 5, 2011 at 18:50
7 Answers
Reset to default 3my_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 ments instead of single-line ments
function () {
/*Right:*/
return 0;
//Wrong:
return 0;
}
After wrapping to a single-line bookmarklet code, the above bees:
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 bees:
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./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.
本文标签: javascriptmissingafter function body even though everything is closed properlyStack Overflow
版权声明:本文标题:javascript - missing } after function body... even though everything is closed properly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741268363a2368883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论