Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

javascript - Why does a script work in Firebug's command line on one site but not on another?

I am using Firefox and Firebug's command line to execute a JavaScript on two different sites:

  1. https://graph.facebook.com/v2.3/172727819415642/albums?fields=id,name,cover_photo,photos%7Bname,source%7D&limit=1&access_token=xxxxx
  2. http://www.iskcondesiretree.com/photo/album/list

Here's the code:

(function() {
   function r() {
       a = $("body").text()
       console.log(a);
   };
   var e = "1.6.4";
   var t = false;
   if (!t) {
       t = true;
       var n = document.createElement("script");
       n.src = "https://ajax.googleapis.com/ajax/libs/jquery/" + e + "/jquery.min.js";
       n.onload = function() {
           r();
       };

       document.getElementsByTagName("head")[0].appendChild(n);
   };
})();

When I run this code in Firebug's command line on site 1, it returns the following error:

TypeError: $(...).text() is not a function

When I run this code site 2 it works fine. It shows lot of text from the site.

Interesting thing is, if I change $ to jQuery it works on site 1, too.

Can anyone tell what's happening? Why Firebug behaves differently for those two sites?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to put your jquery function inside the document ready event.

such like-

$(document).ready(function(){

   // jQuery methods go here...

});

This is to prevent any jQuery code from running before the document is finished loading (is ready).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...